您的位置:首页 > 其它

Beyond Compare as a Diff and Merge tool with Git

2012-11-20 13:10 399 查看



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


?
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


?
This can be configured on a Windows machine similarly:

Windows

notepad C:\Program Files\git\etc\config


?
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


?
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:

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 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: