Diff of current and previous version using vim-fugitive

£可爱£侵袭症+ 提交于 2019-12-03 09:58:43

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 #

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)

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.

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).

One way to see diff of a commit is this:

:Gedit [revision]

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)

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)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!