git中配置Beyond Compare作為mergetool

woshinia發表於2014-01-09
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

相關文章