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

How do I view the git commit history for the current file in vim-fugitive?

Answer

:0Gclog

Explanation

vim-fugitive's :Gclog command loads the git log into the quickfix list, but when prefixed with the range 0 it restricts the history to only the commits that touched the current file. This lets you navigate backward through every revision of a file without leaving Vim.

How it works

  • :Gclog loads all commits into the quickfix list
  • The 0 range (:0Gclog) limits the log to commits that changed the current file — equivalent to git log -- %
  • Each quickfix entry is a commit; pressing <CR> opens that historical version of the file in a fugitive buffer
  • Use :cnext / :cprev (or :cn / :cp) to walk forward and backward through commits
  • From a historical buffer, :Gdiff compares it to the working copy

Example

:0Gclog          " load file history into quickfix
:copen           " open quickfix window to browse commits
:cnext           " jump to the next (older) commit
:cprev           " jump back to the newer commit

Press <CR> on a quickfix entry to open that snapshot. The buffer title shows the commit SHA and filename.

Tips

  • Combine with a range to inspect history for specific lines: :'<,'>Gclog loads only commits that touched the visually selected lines
  • Pass extra git-log flags after the command: :0Gclog --since=2.weeks.ago
  • :Gllog uses the location list instead of quickfix, so each file gets its own list
  • After opening a historical version, :Gread replaces the working buffer with that revision

Next

How do I re-insert the text from my last insert session and immediately return to normal mode?