21

Is there a way of using vimdiff to compare the current version of a file and the same file n git versions ago?

For example, I'm working on the file foo.c on the branch master. I'd like to do a vimdiff between foo.c and foo.c from master~10, to copy one or two lines across from the old version.

PS I'm aware of git cherry pick, but that's for whole files, not just a couple of lines.

Sonia Hamilton
  • 4,229
  • 5
  • 35
  • 50

4 Answers4

19

Duh! RTFM. I was adding a PPS about this not being a use case for git difftool, then started wondering "what exactly does git difftool do...".

Solution is:

git difftool master master~10 -- foo.c

That is, presuming difftool is already set up correctly in ~/.gitconfig:

[diff]
  tool = vimdiff
[difftool]
  prompt = false
Sonia Hamilton
  • 4,229
  • 5
  • 35
  • 50
11

For working with Git under Vim, the fugitive.vim - A Git wrapper so awesome, it should be illegal plugin is very useful. Check it out!

The vimdiff can be made with :Gdiff [revision].

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
8

A solution that works without overriding the git config would be

vimdiff file.tex <( git show master~2:file.tex )
Aaron
  • 321
  • 4
  • 9
3

Possible duplicate of Viewing all `git diffs` with vimdiff

mosh
  • 1,402
  • 15
  • 16
Trevor Norris
  • 20,499
  • 4
  • 26
  • 28