views:

55884

answers:

8

Do you know a good way to explain how to resolve merge conflicts in git?

+4  A: 

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.

Handling and Avoiding Conflicts in Git

mwilliams
+27  A: 
  1. Identify which files are in conflict (Git should tell you this)
  2. 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
  3. Once you've resolved the conflict in a file git add the_file
  4. Once you've resolved all conflicts, do git rebase --continue or whatever command git said to do when you completed
davetron5000
thanks for this simple answer, vey helpful. Any idea why "git add" is used in this case-- seems odd given that the file in question is already in the GIT repo.
Justin Grant
@Justin Think of Git as tracking *content* rather than tracking files. Then it's easy to see that the content you've updated *isn't* in the repository and needs to be added. This way of thinking also explains why Git doesn't track empty folders: Although they are technically files, there isn't any content to track.
Gareth
+7  A: 

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.

Paul
Using "git add" is the real trick here. You may not even want to commit (maybe you want to stash), but you have to do "git add" to complete the merge. I think mergetool does the add for you (although it isn't in the manpage), but if you do the merge manually, you need to use "git add" to complete it (even if you don't want to commit).
nobar
+58  A: 

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.

Peter Burns
Oops, that should be `git mergetool` (no "-")
Pat Notz
Ack, thanks for catching that
Peter Burns
That's the way to go.
e-satis
FYI you can use `git mergetool -y` to save a few keystrokes if you're merging a lot of files at once.
davr
+12  A: 

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
Pat Notz
+23  A: 

The Git manual has some very good instructions, including helpful examples, on handling merge conflicts.

mipadi
Documentation is pretty impenetrable for the average user.
picardo
In a few cases, yet, but the section on resolving merges is fairly clear, I think.
mipadi
-1. If doc does not let you see immidiatly tools like mergetool qnd the --theirs option.
e-satis
This isn't a good answer http://tinyurl.com/3aflwp8. Ironically, second result.
bobobobo
Now the first :/
drhorrible
A: 

Thanks, that helped.

Rom
+7  A: 

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!

CoolAJ86