Git merge without auto commit
GitGit Problem Overview
Is it possible to do a git merge
, but without a commit?
"man git merge" says this:
With --no-commit perform the merge but pretend the merge failed and do not autocommit,
to give the user a chance to inspect and further tweak the merge result before
committing.
But when I try to use git merge
with the --no-commit
it still auto-commits. Here's what I did:
$> ~/git/testrepo$ git checkout master
Switched to branch 'master'
$> ~/git/testrepo$ git branch
* master
v1.0
$> ~/git/testrepo$ git merge --no-commit v1.0
Updating c0c9fd2..18fa02c
Fast-forward
file1 | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
$> ~/git/testrepo$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)
A subsequent git log
reveals all the commits from the v1.0 branch merged into master.
Git Solutions
Solution 1 - Git
Note the output while doing the merge - it is saying Fast Forward
In such situations, you want to do:
git merge <name-of-branch> --no-commit --no-ff
Important: If you do it this way, then you are not able to do any changes to the files in the staging area e.g. you can't remove/add files or make any changes to the files.
If you want to merge the changes and then commit as if you had manually typed all of the changes you merged in (as opposed to a traditional merge) you need to run rm .git/MERGE_HEAD
afterward, which will force git to forget that the merge happened.
Solution 2 - Git
You're misunderstanding the meaning of the merge here.
The --no-commit
prevents the MERGE COMMIT from occuring, and that only happens when you merge two divergent branch histories; in your example that's not the case since Git indicates that it was a "fast-forward" merge and then Git only applies the commits already present on the branch sequentially.
Solution 3 - Git
If you only want to commit all the changes in one commit as if you typed yourself, --squash will do too
$ git merge --squash v1.0
$ git commit
Solution 4 - Git
I prefer this way so I don't need to remember any rare parameters.
git merge branch_name
It will then say your branch is ahead by "#
" commits, you can now pop these commits off and put them into the working changes with the following:
git reset @~#
For example if after the merge it is 1 commit ahead, use:
git reset @~1
Note: On Windows, quotes are needed. (As Josh noted in comments) eg:
git reset "@~1"
Solution 5 - Git
When there is one commit only in the branch, I usually do
git merge branch_name --ff
Solution 6 - Git
Old question with many answers, but this is too big for a comment.
As another answer mentioned, merging v1.0 into master resulted in a fast-forward merge. In fact, there really was no merge. The v1.0 tag had a commit whose parent commit was the tip of master. Got just advanced the pointer for master ahead one commit.
If doing that introduces an bad merge, what you *really" gave us a bad commit at the v1.0 tag.
The more appropriate solution is to do the fast forward merge of v1.0 into master, the add a commit to master correcting the bad code. After that either delete the v1.0 tag and recreate it, or retag v1.0 and force push the tag. Better yet, create a v1.0.1 tag from the commit that fixes v1.0.
Every other answer points you too the wrong solution from a coding standpoint.
Solution 7 - Git
You could also do
git cherry-pick <commit hash>
for each commit if you want to preserve the commit history...
I'm not really seeing a "nice" way to merge multiple commits from another branch with the git merge
command without it adding a merge commit at some point (i.e. only having the commits you want to include)