Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force git show to show diff using vimdiff

Tags:

git

vim

vimdiff

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?

like image 390
username_4567 Avatar asked May 24 '16 08:05

username_4567


People also ask

How do you get to the next difference in Vimdiff?

Switch between diff window Please note that, we have to press Ctrl + w 2 times.

How do I use Vimdiff tool?

Vimdiff can be run from the command-line with vimdiff file1 file2 [file3 [file4]] . This actually starts Vim in diff mode -- the previous command is actually equivalent to vim -d file1 file2 [file3 [file4]] . Vim requires a diff command to be available for this to work.

Why git diff does not show changes?

Your file is already staged to be committed. You can show it's diff using the --cached option of git. To unstage it, just do what git status suggests in it's output ;) You can check The Git Index For more info.


2 Answers

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)
like image 144
diwo Avatar answered Oct 05 '22 23:10

diwo


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.

like image 33
toydarian Avatar answered Oct 05 '22 22:10

toydarian