Question

I know that :Glog will populate the quickfix list with all the versions of the file in the current buffer and that I can hit <enter> on these to see the file at that particular version, but is there an easy way to see the diff of a particular version with the previous version (to easily see what has changed)?

EDIT: What I'm after is seeing each of these changes in a vertical diff in vim: git log -p -- path/to/file.txt.

Was it helpful?

Solution 3

Could not get fugitive to do what I want (it either doesn't do it or I don't know how to use it), but I did find the plugin which gives the desired behaviour: https://github.com/kablamo/vim-git-log.

(it does not show the diff in quickfix list but in a full sized buffer, which is OK)

OTHER TIPS

This is what I use:

:Gdiff [revision]

See :help Gdiff for other options as well (vertical vs horizontal splits, etc).

For example:

:Gdiff HEAD

or if you have a revision number (ie: aaffdfdstsdgssetds):

:Gdiff aaffdfds

I believe it is as simple as:

:vert diffsplit #

First, open the file you want to diff with.

If the change is committed, enter :Gdiff HEAD~1.
If the change is NOT committed, enter :Gdiff HEAD.

To diff with its previous version, follow these steps:

  1. use Glog
  2. navigate to the desired version
  3. type C, which opens the commit
  4. go to the line contains diff --git a/you/file b/you/file and press <cr>

Moreover, to diff a previous version with the version in index (the working version), you can do this:

  1. use Glog
  2. navigate to the desired version
  3. use Gdiff in this new window without parameter. Done!

Gdiff without parameter will diff the current buffer with the one in index, which is exactly what you want

You can also consider using the plugin vim-unimpaired, which provides two maps ]q and [q to navigate the quickfix list. (Also ]Q and [Q)

When using :Glog you can simply press <cr> on the line that starts diff --git. It will open the diff. You may also want to look into :Gdiff. You may want to look at Drew Neil's vimcasts, The Fugitive Series.

For more help see

:h fugitive
:h :Glog
:h :Gdiff

You can achieve this by following these steps:

  1. Change focus to the quickfix window and navigate to the line corresponding to the commit you're interested in.
  2. Find the commit hash in the line and yank it (the hash is now in the 0 register.
  3. Close the quickfix buffer, and do :Gdiff <C-r>0.

That should fire up the proper diff.

You can automate steps 2 and 3 by adding this mapping to your .vimrc file:

nnoremap <Leader>gd /\.git<CR>wwwyw<Esc>:cclose<CR>:Gdiff <C-r>0<CR>

Notice that the mapping assumes your cursor is at the beginning of the line (before the .git// part).

Try :vert Gdiff. This will show the diff of the current file in left-right mode.

One way to see diff of a commit is this:

:Gedit [revision]

I wrote a function that allows you to diff against a previous revision of the current file. I use this to view the diff between the working copy and revisions "n" times back.

Here's the code to put in your .vimrc (requires https://github.com/tpope/vim-fugitive):

"Diff the current file against n revision (instead of n commit)
function! Diffrev(...)

  let a:target = @%

  "check argument count
  if a:0 == 0
    "no revision number specified
    let a:revnum=0
  else
    "revision number specified
    let a:revnum=a:1
  endif

  let a:hash = system('git log -1 --skip='.a:revnum.' --pretty=format:"%h" ' . a:target)
  execute 'Gdiff' . a:hash
endfunc

You can call the function like so (example below diffs against 3rd revision back):

:call Diffrev(2)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top