vimtricks.wiki Concise Vim tricks, one at a time.

How do I view a side-by-side Git diff of staged changes inside Vim?

Answer

:Gdiffsplit

Explanation

The vim-fugitive plugin provides :Gdiffsplit (and its vertical variant :Gvdiffsplit) to open a side-by-side diff view comparing the working tree version of a file against the staged version in the Git index. This turns Vim into a powerful staging tool where you can review, stage, and unstage individual changes using Vim's built-in diff commands.

How it works

With any tracked file open, run:

:Gdiffsplit

This opens a horizontal split with two buffers:

  • Left pane: the staged (index) version of the file
  • Right pane: the working tree version of the file

Use :Gvdiffsplit for a vertical split instead, which is usually more readable.

Staging changes from the diff

This is where fugitive becomes incredibly powerful. You can use Vim's native diff commands to move changes between the working tree and the index:

:diffput   " Push the current hunk from this buffer to the other
:diffget   " Pull the hunk from the other buffer into this one

When you write the index buffer (the staged version), fugitive automatically runs the equivalent of git add for those changes. This gives you full git add -p functionality with the visual precision of Vim's diff mode.

Diffing against other revisions

You can pass any Git revision to diff against:

:Gdiffsplit HEAD~3    " diff against 3 commits ago
:Gdiffsplit main      " diff against the main branch
:Gdiffsplit HEAD:%    " diff against the last committed version

Three-way merge diffs

During a merge conflict, running :Gdiffsplit! (with the bang) opens a three-way diff with the target branch, the working file, and the merge branch side by side. Use :diffget with the buffer number to accept changes from either side.

Tips

  • Use ]c and [c to jump between diff hunks
  • Run :diffupdate if the diff highlighting gets out of sync after edits
  • Press dp as a shortcut for :diffput and do as a shortcut for :diffget
  • Close the diff view with :only or :q on the index buffer to return to normal editing

Next

How do I edit multiple lines at once using multiple cursors in Vim?