5

How do I do this? After doing changes via git config I can diff my staged and committed changes with vimdiff but when I do git show I still see the diff in old plain style. How do I make this work for git show as well?

username_4567
  • 4,737
  • 12
  • 56
  • 92
  • Possible duplicate of http://stackoverflow.com/questions/28048270/how-do-i-use-vim-as-git-show-editor – anujm May 24 '16 at 08:46

4 Answers4

4

Try using git aliases. This is for git show

git config --global alias.s difftool\ HEAD^\ HEAD

And this is for git show <revision>

git config --global alias.s '!f() { rev=${1-HEAD}; git difftool $rev^ $rev; }; f'

To see how it works, get familiar with this page.

Yuri Pozniak
  • 667
  • 7
  • 11
  • Actually this is a pretty good answer and is exactly what I was looking for. This works not only with HEAD but with any commit and it accepts filenames to show with '--'. Disabling the "Launch 'vimdiff' [Y/n]" prompt with 'git config difftool.prompt false' works even better. Thank you. – egelev Apr 04 '19 at 09:55
  • I think the second method is the one I was looking for. Can you explain what the ^ mark means? – Chan Kim Jun 01 '23 at 06:20
  • 1
    Hey @ChanKim , sorry it took me so long. If it still matters, the "^" stands for "parent", so an expression "HEAD^" means the commit that's a parent of HEAD. Or , put it simply, previous commit. You can daisy-chain them like so "HEAD^^^" which means the same as "HEAD~3" which means "3 commits before HEAD". I hope it helps. – Yuri Pozniak Jul 10 '23 at 00:06
3

The default git show with no parameter as well as git show <object> display changes to all files within a commit. Since vimdiff can only compare a single file at a time, you cannot use it with these options.

However git show <object> -- <file> shows the changes to a single file within a commit. You can display the changes in vimdiff by running difftool instead:

git difftool SHA~:SHA -- <file>

If you need more flexibility, you can always use git show to fetch specific versions of a file and pass them into vimdiff via Process Substituion

export FILE=path/to/file; vimdiff <(git show SHA1:$FILE) <(git show SHA2:$FILE)
diwo
  • 336
  • 1
  • 3
3

Following works for me:

git difftool SHA^..SHA -- <file>
Deqing
  • 14,098
  • 15
  • 84
  • 131
2

With git show you can show objects like commits (see the man page for reference). So you don't show a diff of two files, but changes in (maybe multiple) files. So there are not two files, which can be compared. But that is exactly what vimdiff does, it opens two files side-by-side and highlights differences.
When you use git difftool or something like that it will create files for both sides of the diff and use the tool (in your case vimdiff) to compare them. git show does not create those files, so its output can't be shown by vimdiff.

tl;dr: git show is a tool to display git objects, not to create diffs, so its output can't be shown as a diff using vimdiff.

What you might want to do is to use git difftool. It will open gvimdiff for every modified file.
You can use the usual options of git diff to compare different commits.

toydarian
  • 4,246
  • 5
  • 23
  • 35