Edit working tree files during `git difftool` comparison

, Somewhere between Playa del Carmen, Mexico and Boston, MA

There’s a change cooking in git difftool that will make diff tools with editing capabilities much more powerful. The change will make git difftool use symbolic links to working tree files whenever one side of the comparison is the working tree.

Current versions of git difftool can use working tree symlinks, but only for files with uncommitted changes. Otherwise, it creates temporary copies of the files, making it cumbersome to save edits made during the comparison.

Emacs dircmp-mode now follows symbolic links and compares the destination file content, allowing Emacs, together with the git difftool change, to edit working tree files during comparison.

For example, review changes that your topic branch would merge to your master branch:

$ git difftool -d $(git merge-base master HEAD)

dircmp-mode

Make changes, and save them from Ediff:

ediff

Emacs writes the changes directly to your working tree:

$ git diff
diff --git a/git-difftool.perl b/git-difftool.perl
index 5f093ae..7cf61cf 100755
--- a/git-difftool.perl
+++ b/git-difftool.perl
@@ -174,7 +174,8 @@ EOF
        }

        if ($rmode ne $null_mode) {
-           if (use_wt_file($repo, $workdir, $dst_path, $rsha1, $symlinks)) {
+           if (use_wt_file($repo, $workdir, $dst_path, $rsha1,
+                   $symlinks)) {
                push(@working_tree, $dst_path);
            } else {
                $rindex .= "$rmode $rsha1\t$dst_path\0";

To configure dircmp-mode as your Git diff tool:

~/.emacs:

...
(load "/path/to/dircmp/dircmp.el")
...

~/.gitconfig:

...
[difftool "emacs"]
  cmd = /path/to/dircmp/emacs-git-difftool.sh \"$LOCAL\" \"$REMOTE\"
  prompt = false

[diff]
    tool = emacs
...

Thanks to John Keeping, Junio C. Hamano, and David Aguilar for implementing and reviewing the git difftool changes to make this possible.

Enjoyed reading this post? Discuss it on Reddit, or follow me on Twitter.