Dive into Git history with fugitive.vim

fugitive.vim enhances git-blame, and it can also be used to traverse the commit graph

I prefer to use Git via its command line interface, but some tasks just feel too cumbersome to do solely with it. Lately, I needed to use git-blame a lot, but it's not especially handy to use. Because I use Vim a lot, it made sense to look into the features of fugitive.vim and found Gblame that provides multiple ways to quickly find relevant bits of history.

Getting revision information from git-blame

The git-blame from the git CLI opens a new pager to show the revision information and the source code side-by-side.

git-blame from the CLI

Most of the time I need this information when I am already looking at a given line in another editor, wondering why things work the way they do. Leaving the editor and browsing the whole source file in a different presentation is tedious. It also lacks syntax highlighting, so you'll end up looking at a totally different representation of the source file as before.

Another problem is that if you'd like to dive into a commit, you have to type or paste its ID for the next command.

Dive into history with fugitive.vim's Gblame

Luckily, fugitive.vim provides the Gblame command that makes life a bit easier. It shows the revision information right next to the file in question in a panel that's linked to the source file. You can simply jump to the commit that contains the changes for a given line. Looking at the other changes and the commit message it's quite easy to get the context.

Gblame

Some general tips:

  • You can navigate in the blame panel as if it were a normal text file to select a commit.
  • To investigate a commit hit o on a given line. It will open a horizontal split with the changes and the commit message.
  • Alternatively, if you hit <CR> on a commit, it will open it in the current window. I think it's a bit less useful because it navigates away from the current file. However, you can return to the file by pressing Ctrl+^.
  • If you are stuck, press g? to open Gblame's help page directly.
  • Simply press q to dismiss the revision information panel. Alternatively, running :Gblame again closes this window.

So it's convenient, especially if you already have a Vim open. But there are more reasons to use it: you can easily dive deep into the history.

Reblame at commit

Supposing that you saw a suspicious line in the blame information, and you would like to see a version of the file where that change was committed. You can do just that by hitting - after you've selected a commit on the blame window.

Reblame at parent

Another useful feature is to be able to see the evolution of a given line over time. By pressing P you can reblame the file on the parent of the selected commit. This updates the contents of the buffer with the previous version of the file.

Both commands keep the blame tab open and refreshes the information therein, so you can continue to investigate the commits that resulted in the selected version.

Check multiple commits with Gblame

Summary

fugitive.vim is a powerful Git wrapper. I've only started to scratch its surface because I only needed a more handy way to interact with git-blame

Once you start using it, make sure to read its help page (:h fugitive), because it has a very rich functionality so you can find multiple ways to enhance your Git experience.

September 11, 2018

Free PDF guide

Sign up to our newsletter and download the "Foreign key constraints in DynamoDB" guide.


In this article