loading...

Git Diff of Current and Previous Version of a File

serhatteker profile image Serhat Teker Originally published at tech.serhatteker.com on ・1 min read

We will use vim and git wrapper plugin for this.

Installation

First make sure you installed Tim Pope's vim-fugitive plugin before started.

In the repo there is a clear instruction but for vim-plug, add
below line to your .vimrc and source it:

" Plugins will be downloaded under the specified directory.
" Change it to direct to yours 
call plug#begin('~/.local/share/nvim/plugged')

" Add this
Plug 'tpope/vim-fugitive'

call plug#end()

Then run :PlugInstall.

Using

We will run :Gdiffsplit. From documentation:

:Gdiffsplit [object]    Perform a `vimdiff` against the given file, or if a
                        commit is given, the current file in that commit.
                        With no argument, the version in the index or work
                        tree is used.  The newer of the two files is placed to
                        the right or bottom, depending on 'diffopt' and the
                        width of the window relative to 'textwidth'.  Use
                        Vim's `do` and `dp` to stage and unstage changes.

Open the file you want to diff with.

If the changes are NOT committed yet use:

:Gdiffsplit HEAD

But if the changes are committed use:

:Gdiffsplit HEAD~1

Also there are "vertical" and "horizontal" split versions:

:Gvdiffsplit [object]   Like :Gdiffsplit, but always split vertically.

:Ghdiffsplit [object]   Like :Gdiffsplit, but always split horizontally.

For more help:

:help fugitive
:help Gdiffsplit

Also you can always map them as below;

" Git diff current and previous version
nnoremap <leader>d :Gvdiffsplit HEAD<CR>

All done.

Posted on by:

serhatteker profile

Serhat Teker

@serhatteker

Uomo Universale | Software Engineer | Entrepreneur | builds systems | py:js:go |

Discussion

pic
Editor guide