Sathish
Sathish

Reputation: 21080

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

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

How can I use vim-fugitive to git diff

Upvotes: 60

Views: 41596

Answers (5)

matt0089
matt0089

Reputation: 471

By around 2020, with vim-fugitive you can now use

:G difftool -y <revision> <revision>

to compare all files between two revisions (say main branch and HEAD)

It opens a diff for each file in a new tab.

To navigate:

  • gt,gT next,previous tab

  • :tabs,Ngt list tabs, go to tab number N (starting from 1)

  • ]c,[c go to next,previous change in current window

see :h :Git_difftool, :h jumpto-diffs, :h tab-page-commands.
also :h gt and the rest.
see git help difftool for other options/arguments you can pass to :G difftool

Upvotes: 1

AA.
AA.

Reputation: 4606

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.

Upvotes: 43

starikovs
starikovs

Reputation: 3398

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

Upvotes: 5

prathamesh
prathamesh

Reputation: 511

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

Upvotes: 14

Peter Rincker
Peter Rincker

Reputation: 45107

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

Upvotes: 82

Related Questions