Fixing binary merge conflicts in git

Save on DeliciousDigg This
Share on Facebook+1Share on LinkedInPin it on PinterestSubmit to redditSubmit to StumbleUponShare on TumblrShare on Twitter

Git is an awesome tool for managing your code. However, it does take a while to get used to doing things in git. One question I get asked a bit too often is “how do I resolve binary merge conflicts”? Thankfully this is is pretty easy. I usually run the git mergetool command to allow me to resolve merge conflicts using a graphical interface and then manipulate the resultant files on the command line. 

You can specify which editor to use in your ~/.gitconfig file. Favourites are kdiff3vimdiffxxdiff, and opendiff. If your on Mac OSX then you may be used to FileMerge, in which case you should be using opendiff in your .gitconfig; see below.

...
[merge]
tool = opendiff
...



So, when you are using mergetool option you will something like:

Normal merge conflict for 'lib/yui/assets/skins/sam/editor-sprite.gif':
{local}: created
{remote}: created
Hit return to start merge resolution tool (opendiff):



I just hit return so the editor opens, it will probably warn you the file is binary. In a separate terminal window navigate to the directory where the file is in and do an ls; you’ll see something like what is below:

editor-sprite.gif
editor-sprite.gif.BACKUP.41930.gif
editor-sprite.gif.LOCAL.41930.gif
editor-sprite.gif.REMOTE.41930.gif



In this case editor-sprite.gif.REMOTE.41930.gif is the new file and editor-sprite.gif is the original file. To resolve the conflict just copy the REMOTE file over the original file. In my case it would be cp editor-sprite.gif.REMOTE.41930.gif editor-sprite.gif. However, I use the following one liner to resolve these conflicts more generically.

ls *.REMOTE.* | sed “s/\(\(.*\).REMOTE.*\)/cp \1 \2/g” | sh


I have it in shell script in my path so I just run it when I need to resolve these types of conflicts. It just finds files with REMOTE in them and then issues a cp command to copy that file over the original.

Hope this helps!

Outside of git mergetool you can just add the binary files using git add file1 file2 ….

 

Save on DeliciousDigg This
Share on Facebook+1Share on LinkedInPin it on PinterestSubmit to redditSubmit to StumbleUponShare on TumblrShare on Twitter
Tagged with: , , , , , , , ,
Posted in Stuff
3 comments on “Fixing binary merge conflicts in git
  1. Michael says:

    Great post, really useful!

    Quick question, how do you handle Xcode project files, such as the project.pbxproj file? It’s a text files bit it’s advised to treat it as binary.

    With a conflict, do you just select the latest or do you merge with FileMerge?

    Thanks!

  2. Bill says:

    I don’t understand what happens after you copy REMOTE over the original file. Then what? I still have the four image files in my directory. What do I now do with Git?

    thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>