What happens when you type git diff? As with all interesting questions, the answer is “it depends…”
Here’s one thing you want git to do:

Vimdiff!
Step 1: add this to your .gitconfig
[diff]
external = git_diff_wrapper
[pager]
diff =
Step 2: create a file named git_diff_wrapper, put it somewhere in your $PATH
#!/bin/sh
vimdiff "$2" "$5"
I still have access to the default git diff behavior with the --no-ext-diff flag. Here’s a function I put in my bash configuration files:
function git_diff() {
git diff --no-ext-diff -w "$@" | vim -R -
}
- --no-ext-diff : to prevent using vimdiff
- -w : to ignore whitespace
- -R : to start vim in read-only mode
- - : to make vim act as a pager
When it comes to vimdiff, you can get started with this tutorial.
Wonderful, thanks! My team currently uses a “legacy” CVS (and we use “cvsvimdiff” – http://vim.sourceforge.net/scripts/script.php?script_id=1209 ) – but this will find from you will be a significant part of my plan to move to git.
Thanks!
Matt
[...] Git Diff with Vimdiff What happens when you type git diff? As with all interesting questions, the answer is “it [...] [...]
Thanks for -R in vim :). I’ve been using almost the same function in my bash here, but without -R you have to always remember to type :q! instead of :q. Now it’s gone, thank you one more time :).
[...] 24, 2009 by Jonathan Palardy I’ve talked casually about using Vim as a pager before. However, I’m still surprised to see how many people use Vim regularly and don’t know [...]
Thanks for this entry, I found it useful.
Not being ready to go full speed into using vimdiff (i’m just new to it), I put the following in ‘gitvimdiff’. The result is that I can use vimdiff to look at git-diff by running ‘gitvimdiff ‘, but a normal invocation of ‘git-diff’ behaves as I’m used to.h
#!/bin/sh
if [ -n "${GIT_EXTERNAL_DIFF}" ]; then
[ "${GIT_EXTERNAL_DIFF}" = "${0}" ] ||
{ echo “GIT_EXTERNAL_DIFF set to unexpected value” 1>&2; exit 1; }
exec vimdiff “$2″ “$5″
else
GIT_EXTERNAL_DIFF=”${0}” exec git –no-pager diff “$@”
fi
rockin thanks, this works great
I can’t seem to get this to work for the life of me! I just get a warning from vim that the output is not to a terminal. The following is my conifg:
[user]
name = Arthur Axel ‘fREW’ Schmidt
email = ellided@gmail.com
[color]
status = auto
branch = auto
ui = auto
[alias]
ci = commit
co = checkout
[diff]
external = git_diff_wrapper
[pager]
external =
Any ideas what I could be doing wrong?
I just compared with my config.
for the pager section, here’s what I have:
[pager]
diff =
it seems to be “diff” not “external”
let me know how that goes.
Thanks for the help. Works like a charm!
I have a question though: when one has more than 2 files in diff, vimdiff is launched twice. And this gets annoying if you have, say 100 files in diff. Any idea what to do then?
Yeah, this is a common problem. And it’s not Vim specific either.
I usually check how many files are affected with “git status”. Also, vimdiff git diff is _not_ my default choice: I use the “git_diff” alias mentioned at the end of the post.
If you still make that mistake … you can try to kill the git diff process.