How can I view git diff for any commit using vim-fugitive?
Asked Answered
A

4

60

vim-fugitive side-by-side git diff is great for viewing diff of unstaged files.

How can I use vim-fugitive to git diff

  • staged files?
  • any git revision?
Allbee answered 12/3, 2013 at 18:42 Comment(2)
Did you read :h fugitive?Axletree
The question was about how to get :Gstatus + D like functionality for any revision. Edited question.Allbee
B
82

Diff between current file and the index

:Gdiff :0

Diff between current file and some other [revision]

:Gdiff [revision]

Diff between current file and current file 3 commits ago:

:Gdiff ~3
Begun answered 12/3, 2013 at 19:44 Comment(6)
For :Gdiff i need to know the files which were changed in a revision. :Gstatus lists uncommitted files on which i can press D to view the diff. How can i get similar file list and diff on any revision. :Gstatus revision doesn't work!Allbee
I feel like this is the wrong way to approach the problem. You can use :Glog -- to populate the quickfix window with previous commits and explore the commits. I would highly suggest you take a look at the vimcast series on fugitive: vimcasts.org/blog/2011/05/the-fugitive-seriesBegun
These all operate on a single file. If you can stomach the native output of Git, take a look at :Git!. For example, :Git! show [revision] will load a syntax-highlight patch of all files in [revision]. It's not as nice as the custom interaction model of :Gstatus, but can be a handy reference in a split.Sybil
Once you've staged your changes and are typing the commit message inside Gcommit I believe the correct command you want is :Gvdiff ~1 this creates a vertical split which I find easier to see the differences and also more space efficient as the top pane is then still your commit message and your diff is a vertical split below thatPena
@Pena you can also do :Gcommit --verbose or cvc from the :Gstatus window.Begun
adding to @Sybil 's comment above, the output of :Git show [commithash] is actually somewhat interactive. Presing ENTER on a diff summary line for example will give a side-by-side diff of the file. more at vimcasts.org/episodes/…Inserted
A
43

You can use :Glog to get the history changes for current file. You can use :cnext or :cprevious to move between changes. When you hit the version that you want to compare then you can use :Gdiff. You can exit from vimdiff closing the buffer :q and exit from history log with :Gedit.

This is my .vimrc keyboard config:

  nnoremap <leader>gs :Gstatus<CR>
  nnoremap <leader>gc :Gcommit -v -q<CR>
  nnoremap <leader>ga :Gcommit --amend<CR>
  nnoremap <leader>gt :Gcommit -v -q %<CR>
  nnoremap <leader>gd :Gdiff<CR>
  nnoremap <leader>ge :Gedit<CR>
  nnoremap <leader>gr :Gread<CR>
  nnoremap <leader>gw :Gwrite<CR><CR>
  nnoremap <leader>gl :silent! Glog<CR>
  nnoremap <leader>gp :Ggrep<Space>
  nnoremap <leader>gm :Gmove<Space>
  nnoremap <leader>gb :Git branch<Space>
  nnoremap <leader>go :Git checkout<Space>
  nnoremap <leader>gps :Dispatch! git push<CR>
  nnoremap <leader>gpl :Dispatch! git pull<CR>

I recommend unimpaired.vim plugin by Tim Pope.

With that configuration, my workflow is:

<Leader>gl to view history

]q and [q to move between versions (unimpaired.vim)

<Leader>gd to open diff

:q to end diff

<Leader>ge to return to my working copy.

Atworth answered 7/3, 2015 at 16:16 Comment(2)
also requires vim-dispatchLilalilac
For those looking at this on 2023. The right command to look at the previous versions of the file is "0Gclog" (instead of Glog), then do GdiffAlanna
G
14

Adding to above answer:

If you want to get a diff, of a particular file from another branch

Gdiff branch_name:path/to/dir/filename.txt
Giselle answered 12/1, 2015 at 6:57 Comment(1)
You can also go Gdiff <branch> on a buffer if the same file is also present in the <branch>.Siva
C
5

I just drop here the way I view a diff if the :Glog -- or :Glog -- % (for current file) is used:

  1. :Glog --
  2. :cw to open a list of commits
  3. Navigate to a commit and its info as well affected files are shown
  4. Focus on a file path
  5. <c-w>gf and the diff is opened in new tab
  6. :tabclose to just close the tab and get the previous state before the diff
Crittenden answered 13/7, 2017 at 8:18 Comment(2)
Hi there. This worked perfect. But what is the meaning of gf here? I don't find it in fugitive documentation.Lowson
Hey! gf means editing the existing file under the cursor in the same window.Crittenden

© 2022 - 2024 — McMap. All rights reserved.