DEV Community

Serhat Teker
Serhat Teker

Posted on • Originally published at tech.serhatteker.com on

Git Diff of Current and Previous Version of a File

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()
Enter fullscreen mode Exit fullscreen mode

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.

Enter fullscreen mode Exit fullscreen mode

Open the file you want to diff with.

If the changes are NOT committed yet use:

:Gdiffsplit HEAD
Enter fullscreen mode Exit fullscreen mode

But if the changes are committed use:

:Gdiffsplit HEAD~1
Enter fullscreen mode Exit fullscreen mode

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

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

:Ghdiffsplit [object]   Like :Gdiffsplit, but always split horizontally.
Enter fullscreen mode Exit fullscreen mode

For more help:

:help fugitive
:help Gdiffsplit
Enter fullscreen mode Exit fullscreen mode

Also you can always map them as below;

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

All done.

Top comments (0)