Git Diff of Current and Previous Version of a File
We will use
git wrapper plugin for this.
First make sure you installed Tim Pope’s vim-figitive plugin before started.
In the repo there is 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()
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:
But if the changes are committed use:
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 it as below;
" Git diff current and previous version nnoremap <leader>d :Gvdiffsplit HEAD<CR>