How do I finish the merge after resolving my merge conflicts?
Question
I've read the Basic Branching and Merging section of the Git Community Book.
So I follow it and create one branch: experimental
.
Then I:
- switch to experimental branch (git checkout experimental)
- make a bunch of changes
- commit it (git commit -a)
- switch to master branch (git checkout master)
- make some changes and commit there
- switch back to experimental (git checkout experimental)
- merge master change to experimental (git merge master)
there are some conflicts but after I resolve them, I did 'git add myfile'
And now i am stuck, I can't move back to master
when I do
$ git checkout master
error: Entry 'res/layout/my_item.xml' would be overwritten by merge. Cannot merge.
and I did:
$ git rebase --abort
No rebase in progress?
and I did :
$ git add res/layout/socialhub_list_item.xml
$ git checkout master
error: Entry 'res/layout/my_item.xml' would be overwritten by merge. Cannot merge.
What can I do so that I can go back to my master branch?
Accepted Answer
When there is a conflict during a merge, you have to finish the merge commit manually. It sounds like you've done the first two steps, to edit the files that conflicted and then run git add
on them to mark them as resolved. Finally, you need to actually commit the merge with git commit
. At that point you will be able to switch branches again.
Popular Answer
How do I finish the merge after resolving my merge conflicts?
With Git 2.12 (Q1 2017), you will have the more natural command:
git merge --continue
See commit c7d227d (15 Dec 2016) by Jeff King (peff
).
See commit 042e290, commit c261a87, commit 367ff69 (14 Dec 2016) by Chris Packham (cpackham
).
(Merged by Junio C Hamano -- gitster
-- in commit 05f6e1b, 27 Dec 2016)
See 2.12 release notes.
merge
: add '--continue
' option as a synonym for 'git commit
'Teach '
git merge
' the--continue
option which allows 'continuing' a merge by completing it.
The traditional way of completing a merge after resolving conflicts is to use 'git commit
'.
Now with commands like 'git rebase
' and 'git cherry-pick
' having a '--continue
' option adding such an option to 'git merge
' presents a consistent UI.
Read more... Read less...
In case you ever get stuck during a merge/rebase you can always
git reset --hard
to restore your working to the state of the last commit. This will lose your changes from the working tree so if you had local modifications before the merge they will be gone after this—which is why it’s advisable to not start a merge when you have local modifications. :)
Just git commit
it.
Optionally git abort
it:
I ran into a merge conflict. How can I abort the merge?
To make life easier with on merges install kdiff3 and configure it as a mergetool. Instructions: http://doodkin.com/2016/05/29/git-merge-easy-github-this-branch-has-conflicts-that-must-be-resolved-use-the-command-line/
That page contains this video: https://www.youtube.com/watch?v=Cc4xPp7Iuzo
Whenever You merge two branches using command git merge brancha branchb
, There are two possibilities:
One branch (lets say brancha) can be reached by the other branch (lets say branchb) by following its commits history.In this case git simply fast-forward the head to point to the recent branch (in this case branchb).
2.But if the two branches have diverged at some older point then git creates a new snapshot and add a new commit that points to it. So in case there is no conflict between the branches you are merging, git smoothly creates a new commit.
Run
git log
to see the commit after you have merged two non-conflicting branches.
Now coming back to the interesting case when there are merge conflicts between the merging branches. I quote this from the page https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging
Git hasn’t automatically created a new merge commit. It has paused the process while you resolve the conflict. If you want to see which files are unmerged at any point after a merge conflict, you can run
git status
So in case there are merge conflicts, you need to resolve the conflict then add the changes you have made to the staging area using git add filename
and then commit the changes by using the command git commit
which was paused by git because of the conflict.I hope this explains your query. Also do visit the link above for a detailed understanding. In case of any query please comment below , I'll be happy to help.
The next steps after resolving the conflicts manually are:-
- git add .
- git status (this will show you which commands are necessary to continue automatic merge procedure)
- [command git suggests, e.g.
git merge --continue
,git cherry-pick --continue
,git rebase --continue
]
A merge conflict occurs when two branches you're trying to merge both changed the same part of the same file. You can generate a list of conflicts with git status
.
When the conflicted line is encountered, Git will edit the content of the affected files with visual indicators that mark both sides of the conflicting content.
<<<<<<< HEAD
conflicted text from HEAD
=======
conflicted text from merging_branch
>>>>>>> merging_branch
When you fix your conflicted files and you are ready to merge, all you have to do is run git add
and git commit
to generate the merge commit. Once the commit was made ,git push
the changes to the branch.
Reference article: Git merge.