How to Use Kdiff3 as a 3-way Merge Tool With Mercurial, Git, and Tower.app

Jan 12th, 2012                 | Comments

There are a few very nice looking, mac-like diff tools for OSX (Kaleidoscope and
Changes come to mind), but none for doing “real” merges.  By this, I mean real,
3-way merges with all of the information you need in front of you.

There are no good-looking, “mac-like” merge tools, but if you swallow your pride there are a few different options for 3-way merges, including
Araxis Merge ($$$!),
DiffMerge,
DeltaWalker, and
FileMerge which comes free with XCode.

I’ve tried them all, and find them all confusing.  They all tend use a 3-pane display to do the merging with your file in the left pane, the file you’re merging in the right pane, and the messy half-merged file in the middle.

That’s not enough information.

A 3-way merge actually has four important sources of information:

  • LOCAL - your file with the changes you’ve made to it
  • REMOTE - the file you’re merging in, possibly authored by someone else
  • BASE - the common ancestor file that LOCAL and
    REMOTE
    came from
  • MERGE_RESULT - the file resulting from the merge where you resolve conflicts

You often need to see all four of these pieces of information to make intelligent choices.  Where you came from (LOCAL), where the other person’s changes came from (REMOTE), where you both started (BASE) and where you
are now (MERGE_RESULT).

Most other 3-way merge tools either conflate or omit the BASE and that can make it harder to see what the right thing to do is.

Kdiff3 is my merge tool of choice.  It’s not pretty; it’s cross-platform
Qt based so it has a very old-school linux GUI feel to it.  But like linux, it’s functional and can help you quickly be productive once you get over the learning curve.

If you’ve got 2 heads that you need to merge in your current repository: We made this modification to
file.txt:

diff --git a/file.txt b/file.txt
--- a/file.txt
+++ b/file.txt
@@ -1,1 +1,1 @@
-BASE: common_ancestor
+LOCAL: file in current working branch

and they made this change to the same file and line:

diff --git a/file.txt b/file.txt
--- a/file.txt
+++ b/file.txt
@@ -1,1 +1,1 @@
-BASE: common_ancestor
+REMOTE: other branch we're merging in

If you run your SCM’s merge command (here, in mercurial: hg merge -r 2), and have kdiff3 configured as your merge tool, you’ll get a pop-up window like this:
As you can see, it shows you all 4 pieces of information,
BASE, LOCAL, and REMOTE on top, and the
MERGE_RESULT
file on the bottom.  It currently has a Merge Conflict that you need to fix.

You can move from one unresolved conflict to the next using the triple up and triple-down colored arrows in the middle of the tool bar.  When a conflict is highlighted, you can press any combination of the A, B, and C buttons in the toolbar.  Pressing one
of those buttons will resolve the conflict with the code from pane A, B, or C on top.  So if the
LOCAL file (your file) had the right changes in it, you’d press B.

It’s possible to press more than one button if code from multiple panes is valid.  You can also directly edit the file in the
MERGE_RESULT pane to make manual changes if the correct merge is not the exact text in A/B/C.

Another option, if you want to take all of the changes from one file and discard any changes from the others, is to go to the “Merge” menu and pick one of “Choose A Everywhere”, “Choose B Everywhere”, or “Choose C Everywhere”.

Once you’ve resolved your file, simply save it (cmd-S) and quit out of kdiff3.  Your SCM should see the
MERGE_RESULT no longer has any merge conflicts and will mark it as resolved, ready for you to commit it.  If there are other files with merge conflicts, you can repeat the process with those files.

Installing kdiff3

Installing kdiff3 is as easy as downloading the latest version from sourceforge and copying it to your
/Applications
directory.

The app is a simple wrapper around a Qt-based application.  It can be run from the command line at
/Applications/kdiff3.app/Contents/MacOS/kdiff3.  You can make a symlink of that into your path, but I assume in the instructions below only that you’ve got kdiff3 in your
/Applications folder.

Mercurial Integration

Mercurial command line integration is pretty easy.  Just open up your ~/.hgrc file (or create one if you don’t have it already), and add this to it:

[extdiff]
cmd.kdiff3 = /Applications/kdiff3.app/Contents/MacOS/kdiff3

[merge-tools]
kdiff3.args = $base $local $other -o $output

That configures kdiff3 as your merge tool of choice, so it should pop up automatically when you hit a merge conflict.  You can also use it as a diff tool:

hg kdiff3 -r revision_to_compare_to

Git Command-Line Integration

To configure the git command line to use kdiff3 as a diff and merge tool, add this to your ~/.gitconfig:

[difftool "kdiff3"]
    path = /Applications/kdiff3.app/Contents/MacOS/kdiff3
    trustExitCode = false
[difftool]
    prompt = false
[diff]
    tool = kdiff3
[mergetool "kdiff3"]
    path = /Applications/kdiff3.app/Contents/MacOS/kdiff3
    trustExitCode = false
[mergetool]
    keepBackup = false
[merge]
    tool = kdiff3

Now you can use the external tool commands to look at diffs:

git difftool [revision_sha]

and fix unresolved merge conflicts:

git mergetool

Git Tower Integration

I’m normally a pretty hard-core command line user, but I still sometimes find the places I get myself in git confusing enough that I’m willing to use a GUI to get myself out.  I think that
git-tower is the best git GUI available right now.

It can show you diff files and lets you browse through history in it’s UI. It also lets you do a number of basic and mid-level commands (adding, committing, rebasing, squashing commits, cherry-picking, etc), but it doesn’t have a built-in diff or merge tool.

It does come with the ability to integrate with a few different merge tools out of the box, but not kdiff3.

After a little digging around, I found a generic post showing how configure Tower with a plist file and a shim shell script, but nothing specific to kdiff3.

I figured out how to get Tower and kdiff3 to play nice with each other, and have the instructions and files in a
GitHub repository.  To get it working, just clone the repository to a temporary directory, and run the install.sh script:

cd /tmp
git clone git://github.com/tednaleid/git-tower-kdiff3-shim.git
cd git-tower-kdiff3-shim
./install.sh

It simply copies the plist file and the shim script into the appropriate directories (and Tower is
very picky about those locations).

Then you need to go into Tower’s Preferences->Git Config control panel, and choose kdiff3 as the diff and merge tool:
For a quick rundown of how Tower works with kdiff3 as a merge tool, here’s an example where
we’ve got one commit where we made changes to locally: And another commit on
origin/master where someone else made changes to the same piece of code:
We’ve pulled it down
and now have 2 heads within our repository. To fix this, we need to merge and resolve the conflicts, hit the merge button and we’ll get an error message because there are conflicts that git can’t resolve automatically:
If you highlight
a file with merge conflicts, and then hit the “Merge Tool” button, it will bring up kdiff3 and let us use it to resolve the issue:
Fix the merge conflicts
in kdiff3 (ex: press “C” to change it to “Goodbye Cruel World”), save it, and quit out of kdiff3. Tower should see that the file has had it’s conflicts resolved and lets you commit the merge and carry on.

You can also use Kdiff3 as a regular diff tool if you don’t like looking at diff files.  Just choose a file that you’ve modified to diff:
Press the “Diff Tool” button,
and it’ll pop up in kdiff3: Kdiff3 might be homely, but it’s easy to use once you understand how it works.

Copied from: http://naleid.com/blog/2012/01/12/how-to-use-kdiff3-as-a-3-way-merge-tool-with-mercurial-git-and-tower-app/

How to Use Kdiff3 as a 3-way Merge Tool With Mercurial, Git, and Tower.app的更多相关文章

  1. win10下怎么配置以KDiff3作为merge tool和diff tool

    系统环境: OS: Windows 10 Git 2.6.1.windows.1 KDiff3 0.9.98 (64 bit) 具体代码如下: git config --global --add me ...

  2. Git进一步学习

    Git 安装配置 在使用Git前我们需要先安装 Git.Git 目前支持 Linux/Unix.Solaris.Mac和 Windows 平台上运行. Git 各平台安装包下载地址为:http://g ...

  3. 开源文件比较工具:WinMerge、KDiff3、diffuse

    为了寻找免费的BeyondCompare的替代品,最后经过实用,找到如下一些: 1.diffuse 感受:如果仅仅是比较两个文本类的文件,这个软件也就够用了. 安装好后,对着文件点击右键,会出现“Op ...

  4. 开源的文件比较工具:WinMerge,KDiff3,diffuse

    为了寻找免费的BeyondCompare的替代品,最后经过实用,找到如下一些: 1.diffuse 感受:如果仅仅是比较两个文本类的文件,这个软件也就够用了. 安装好后,对着文件点击右键,会出现&qu ...

  5. Git学习笔记--- diff工具 kdiff3

    图形化的git diff 与 git merge工具:kdiff3 1.安装 win10: 去官网左边找到Download,下载双击安装. Linux(Debian Ubuntu 18.04): ap ...

  6. KDiff3使用指南

    http://kdiff3.sourceforge.net/ KDiff3 is a diff and merge program that compares or merges two or thr ...

  7. KDiff3 Merge工具的使用 极简教程

    www.swack.cn - 原文链接:KDiff3 Merge工具 1.软件安装 下载链接:KDiff3-64bit-Setup_0.9.98-2.exe 安装KDiff3 git config - ...

  8. 在春意盎然的季节里初识GIT

    Git 与 SVN 区别 GIT不仅仅是个版本控制系统,它也是个内容管理系统(CMS),工作管理系统等. 如果你是一个具有使用SVN背景的人,你需要做一定的思想转换,来适应GIT提供的一些概念和特征. ...

  9. Git 基本概念及常用命令

    一.基本概念 文件的三种状态:(任何一个文件在git中都有以下三种状态) 1) 已提交(committed):表示该文件已经被安全地保存在本地数据库中了. 2) 已修改(modified):表示修改了 ...

随机推荐

  1. Windows2008 R2 Enterprise离线安装IE10和VS2015过程记录

    直接下载IE10,进行安装,提示需要联机下载更新: 在网上搜索到一篇文章(http://www.cnblogs.com/nbpowerboy/p/3383992.html),参考 以下载简体中文的Wi ...

  2. [读书笔记]C#学习笔记四: C#2.0泛型 可控类型 匿名方法和迭代器

    前言 C#1.0的委托特性使方法作为其他方法的参数来传递,而C#2.0 中提出的泛型特性则使类型可以被参数化,从而不必再为不同的类型提供特殊版本的实现方法.另外C#2.0还提出了可空类型,匿名方法和迭 ...

  3. 机器学习技法-GBDT算法

    课程地址:https://class.coursera.org/ntumltwo-002/lecture 之前看过别人的竞赛视频,知道GBDT这个算法应用十分广泛.林在第八讲,简单的介绍了AdaBoo ...

  4. java_queue

    队列是一种特殊的线性表,先进先出(first in first out)FIFO,它只允许在表的前端(front)进行删除操作,只允许在表的后端(rear)进行插入操作. 实际应用:排队等待公交车,银 ...

  5. 海外支付:抵御信用卡欺诈的CyberSource

    海外支付:抵御信用卡欺诈的CyberSource 吴剑 2014-06-04 原创文章,转载必需注明出处:http://www.cnblogs.com/wu-jian 吴剑 http://www.cn ...

  6. java 考试试题

    Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语法,集合的语法,io 的语法,虚拟机方面的语法,其他.有些题来自网上搜集整理,有些题来自学员 ...

  7. Linux文件类型与扩展名

    Linux文件类型和Linux文件的文件名所代表的意义是两个不同的概念.我们通过一般应用程序而创建的比如file.txt.file.tar.gz ,这些文件虽然要用不同的程序来打开,但放在Linux文 ...

  8. Facebook开源的基于SQL的操作系统检测和监控框架:osquery daemon详解

    osqueryd osqueryd(osquery daemon)是可以定期执行SQL查询和记录系统状态改变的驻守程序. osqueryd能够根据配置手机归档查询结果,并产生日志. 同时也可以使用系统 ...

  9. Exp5 Msf基础应用 20164312 马孝涛

    1.本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 1.1一个主动攻击实践,如ms08_067; (1分) 1.2 一个针对浏览器的攻击,如ms11_ ...

  10. python进程间通信

    Process之间有时需要通信,操作系统提供了很多机制来实现进程间的通信. 1. Queue的使用 可以使用multiprocessing模块的Queue实现多进程之间的数据传递,Queue本身是一个 ...