Do you know a good way to explain how to resolve merge conflicts in git?
The following blog post seems to give a very good example on how to handle merge conflict with Git that should get you going in the right direction.
- Identify which files are in conflict (Git should tell you this)
- Open each file and examine the diffs; Git demarcates them. Hopefully it will be obvious which version of each block to keep. You may need to discuss it with fellow developers who committed the code
- Once you've resolved the conflict in a file
git add the_file
- Once you've resolved all conflicts, do
git rebase --continue
or whatever command git said to do when you completed
If you're making frequent small commits, then start by looking at the commit comments with git log --merge
. Then git diff
will show you the conflicts.
For conflicts that involve more than a few lines, it's easier to see what's going on in an external gui tool. I like opendiff -- git also supports vimdiff, gvimdiff, kdiff3, tkdiff, meld, xxdiff, emerge out of the box and you can install others: git config merge.tool "your.tool"
will set your chosen tool and then git mergetool
after a failed merge will show you the diffs in context.
Each time you edit a file to resolve a conflict git add filename
will update the index and your diff will no longer show it. When all the conflicts are handled and their files have been git add
-ed, git commit
will complete your merge.
Try: git mergetool
It opens a GUI that steps you through each conflict and you get to choose how to merge. Sometimes it requires a bit of hand editing afterwords, but usually it's enough by itself. Much better than doing the whole thing by hand certainly.
Checkout the answers in Aborting a merge in git especially this one which shows how to view the different versions of the file with problems, e.g.,
# common base version of the file
git show :1:some_file.cpp
# 'ours' version of the file
git show :2:some_file.cpp
# 'theirs' version of the file
git show :3:some_file.cpp
The Git manual has some very good instructions, including helpful examples, on handling merge conflicts.
Here's a probable use-case, from the top:
You're going to pull some changes, but oops, you're not up to date:
git fetch origin
git pull origin master
From ssh://[email protected]:22/projectname
* branch master -> FETCH_HEAD
Updating a030c3a..ee25213
error: Entry 'filename.c' not uptodate. Cannot merge.
So you get up-to-date and try again, but have a conflict:
git add filename.c
git commit -m "made some wild and crazy changes"
From ssh://[email protected]:22/projectname
* branch master -> FETCH_HEAD
Auto-merging filename.c
CONFLICT (content): Merge conflict in filename.c
Automatic merge failed; fix conflicts and then commit the result.
So you decide to take a look at the changes:
git mergetool
Oh me, oh my, upstream changed some things, but just to use my changes.... no... their changes...
git checkout --ours filename.c
git checkout --theirs filename.c
git add filename.c
git commit -m "using theirs"
And then we try a final time
git pull origin master
From ssh://[email protected]:22/projectname
* branch master -> FETCH_HEAD
Already up-to-date.
Ta-da!