How do I open the current file at a previous Git revision with vim-fugitive?
Answer
:Gedit HEAD~1:%
Explanation
When you are deep in a refactor, you often need to compare the current buffer with an older version of the same file. :Gedit HEAD~1:% from vim-fugitive opens that historical file directly in Vim without leaving your editing flow. This is faster than dropping to shell commands and manually locating a path at a commit.
How it works
:Geditis fugitive's file-opening command for Git objects and working tree pathsHEAD~1means the commit one parent behind currentHEAD:%means "the current file path" relative to the repo, so you do not need to type it
Together, HEAD~1:% resolves to "this same file, but at the previous commit".
Example
You are editing:
app/services/invoice_builder.rb
Run:
:Gedit HEAD~1:%
Now the buffer shows the previous-commit version of invoice_builder.rb for quick inspection or manual copy/paste into your current branch.
Tips
- Use
:Gedit main:%to view the version from your main branch - Use
:Gdiffsplit HEAD~1when you want side-by-side hunk navigation - Pair with
:Gwriteif you intentionally want to stage the current buffer afterward