Beyond Compare as a Diff and Merge tool with Git

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
1
2
3
4
#!/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 bc3
git 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[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
1
2
3
4
5
6
7
8
9
10
11
12
[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 would be:
  1. git merge
  2. git mergetool -t [tool]
  3. git add .
  4. git commit
For example:
Pull changes on a file that has been modified by another user:
git fetch origin
git pull origin master
From github.com:domain/project
* branch            master     -> FETCH_HEAD
Updating c44e43e..b3813c5
error: Entry 'filename.php' not uptodate. Cannot merge.
Attempt to update your changes with automatic merging:
git add filename.php
git commit -m "made x changes"     
From github.com:domain/project
 * branch            master     -> FETCH_HEAD
Auto-merging filename.php
CONFLICT (content): Merge conflict in filename.php
Automatic 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.php
git checkout --theirs filename.php
Commit the changes:
git add filename.php
git commit -m "made x changes"
Verify:
git pull origin master
From github.com:domain/project
 * branch            master     -> FETCH_HEAD
Already 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
Tags: 

Comments

 

Charles,
A user of Beyond Compare linked to this post in our forums. Your kind words about our tool are much appreciated. I did want to note however that there are actually two command line tools associated with calling Beyond Compare that are slightly different in use.

The two tools are 'bcompare' and 'bcomp'. 'bcompare' is intended for direct use from the CLI and returns immediately after passing arguments to the main Beyond Compare executable. 'bcomp' passes the arguments to the main executable but then waits for a results value to come back before terminating.

Therefore 'bcomp' is the tool that should be called when using Beyond Compare as an external diff with git (or anything else). 'bcomp' will stay around to catch the result from the main executable and pass that result to the calling app. It will also let the calling app know that Beyond Compare is still using any temporary files created and therefore they should not be removed until Beyond Compare is done with them.

so git-diff-wrapper above should be:
"/usr/bin/bcomp" "$2" "$5" | cat

(Notice that your example for Windows above is calling the BComp.exe tool appropriately).

Also - we do have a MacOSX version on the way. It was an alpha tester that linked to this post.

Thanks
David Jenkins
Scooter Software


转载链接:http://www.iokom.com/drupal/node/4

posted @ 2013-11-29 14:11  顽强的绿萝  阅读(947)  评论(0)    收藏  举报