How to compare file revisions with fugitive.vim

Augment git-log with fugitive to speed up history analysis.

With git-log it's easy to quickly skim history in the form of patches, but it's a bit harder to have a quick look at different revisions of a file or to see the difference between two arbitrary versions. fugitive.vim provides tools to do just that.

View previous file revisions

Glog is a wrapper around git log <current file>. Upon execution it shows all commits that affect the opened file, then it loads the latest revision into the buffer. It also populates the quickfix list with the latest revisions of the file.

There are multiple options to investigate the file history.

You can use copen right after Glog to open the quickfix window where you can select any version to load it into the buffer.

Revisions in a quickfix window

As an alternative, you can use cnext and cprev to cycle through the revisions of the file.

The revisions are presented in a read-only buffer. You can return to the working copy of the file with the Gedit command.

Some tips:

  • If the file has a long history that you are not interested in you can limit the results by supplying additional parameters to Glog. For example, Glog -10 returns the last 10 entries only. For more options, check out help fugitive-:Glog.
  • The vim-airline plays nicely with fugitive.vim. It shows the commit hash when you are viewing older versions and reminds you that you are viewing a read-only buffer.
  • For more examples check out this Vimcast.

Difference between revisions

After you've selected a revision, you can easily compare it to any other version with Gdiff.

  • Gdiff, with no arguments: compare with the working copy.
  • Gdiff HEAD: compare to the latest revision.
  • Gdiff <rev>: compare to an arbitrary revision. As usual, it's enough to provide the first few characters of the SHA-1 hash.
two-way diff between revisions

Use Gvdiff instead of Gdiff to open the diff vertically aligned.

Summary

fugitive.vim nicely augments the command-line git workflow. Its commands are oriented around the currently edited file, which makes it handy for many tasks, for example checking file history or revision information.

September 25, 2018
In this article