Diff of current and previous version using vim-fugitive

喜你入骨 提交于 2019-12-21 03:17:17

问题


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.


回答1:


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



回答2:


I believe it is as simple as:

:vert diffsplit #



回答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)




回答4:


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.




回答5:


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



回答6:


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




回答7:


One way to see diff of a commit is this:

:Gedit [revision]



回答8:


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)




回答9:


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)



来源:https://stackoverflow.com/questions/16802629/diff-of-current-and-previous-version-using-vim-fugitive

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