One of the first real points of frustration a developer encounters with git is the initial unresolved merge conflict.
Beyond compare is an excellent file Comparison Utility and can be configured with git as a merge and diff tool. to Setup diff on Linux, create a short wrapper script to pass the parameters in the correct order:
vi ~/git-diff-wrapper
?
1234 |
#!/bin/sh # diff is called by git with 7 parameters: # path old-file old-hex old-mode new-file new-hex new-mode "/usr/bin/bcompare"
"$2" "$5" |
cat |
Windows users can configure this by entering the commands:
git config --global diff.tool bc3git config --global difftool.bc3.path "C:\Program Files (x86)\Beyond Compare 3\BComp.exe"
Now edit your git config using the sample below to configure merging and use of the script above: Linux
vi ~/.gitconfig
?
12345678910111213141516171819 |
[user] name = First Last email = <a href="mailto:email@address.com">email@address.com</a> [color] ui = true [core] editor = nano [diff] external = ~/git-diff-wrapper [merge] tool = bc3 [mergetool "bc3"] cmd = bcompare \ "$PWD/$LOCAL" \ "$PWD/$REMOTE" \ "$PWD/$BASE" \ "$PWD/$MERGED"
keepBackup = false trustExitCode = false |
This can be configured on a Windows machine similarly: Windows
notepad C:\Program Files\git\etc\config
?
1234567891011121314151617 |
[user] name = First Last email = <a href="mailto:email@address.com">email@address.com</a> [color] ui = true [core] editor = nano [merge] tool = bc3 [mergetool "bc3"] cmd = 'C:\Program Files (x86)\Beyond Compare 3\BComp.exe' \ "$PWD/$LOCAL" \ "$PWD/$REMOTE" \ "$PWD/$BASE" \ "$PWD/$MERGED"
keepBackup = false trustExitCode = false |
Beyond compare is not available for Mac OS X, checkout
Diffmerge for a similar solution. Here's a sample configuration: OSX w/diffmerge
vi ~/.gitconfig
?
123456789101112 |
[user] name = First Last email = <a href="mailto:email@address.com">email@address.com</a> [color] ui = true [core] editor = vi [merge] tool = diffmerge [mergetool "diffmerge"] cmd = diffmerge --merge --result=$MERGED $LOCAL $BASE $REMOTE trustExitCode = false |
Note the command line accepts 4 parameters:
- Local-current branch version
- Remote-version to be merged
- Base-common ancestor
- Merged-file where results will be written
Now you can use beyond compare for diff (GIT diff) and to handle conflicts. The sequence of commands for a merge using mergetool wocould be:
- Git merge
- Git mergetool-T [Tool]
- Git add.
- Git commit
For example: Pull changes on a file that has been modified by another user:
git fetch origingit pull origin masterFrom github.com:domain/project* branch master -> FETCH_HEADUpdating c44e43e..b3813c5error: Entry 'filename.php' not uptodate. Cannot merge.
Attempt to update your changes with automatic merging:
git add filename.phpgit commit -m "made x changes" From github.com:domain/project * branch master -> FETCH_HEADAuto-merging filename.phpCONFLICT (content): Merge conflict in filename.phpAutomatic merge failed; fix conflicts and then commit the result.
Now merge using beyond compare:
git mergetool
If you complete the merge and save it, mergetool will accept the result. If not, you can accept changes, or use another version Wholesale:
git checkout --ours filename.phpgit checkout --theirs filename.php
Commit the changes:
git add filename.phpgit commit -m "made x changes"
Verify:
git pull origin masterFrom github.com:domain/project * branch master -> FETCH_HEADAlready up-to-date.
Git mergetool also includes preconfigured support for a number of open source merge tools: kdiff3, tkdiff, meld, xxdiff, emerge, vimdiff, gvimdiff, ecmerge, and opendiff. these can be used with the-T flag:
git mergetool -t kdiff3