Vim Plugins: Fugitive

Fugitive is awesome, but incredibly complicated!

Use :help fugitive within vim (when the fugitive plugin is installed) for comprehensive documentation. You can also type g? during an interactive command to get context sensitive help.

Here are my favourite things.

You can:

:Git <command>
:Git rebase -i main
:Git commit

You can run basically any git command from within vim. File editing and stdout are handled intelligently. If you don’t supply a command, the default is status. This provides the git status information, but you can manipulate the status within the buffer. So, you can view the diff/status of a file with = and (un)stage a file with -. <enter> will open the file under the cursor. c will initiate a git commit

:Git blame

Do a git blame on the current file, and show the results alongside each line. You can press return on any of the commits to see the full git commit message and patch information.

:Gclog

View log and interact and navigate through branches, commit IDs and diffs

:Gedit branchname:/path/to/file

is used to open a read-only copy of any file on any branch

:Gedit branch:% etc
:Gedit <sha> - to view the commit info, interactively. <enter> on objects to see more
:Ggrep <grep expression>

Do a git grep as specified, and show the result in a quickfix buffer. You can navigate that buffer and press enter to open the file.

:Gdiff - run a vimdiff on the current file (index | working copy)
  :Gwrite - write the working copy to the index, or vice versa!
  :Gread - the other way round
  :diffget - (a vim command) get the change from the other diffed file) (do)
  :diffput - (a vim command) put the change to the other diffed file) (dp)
  :diffupdate - update vimdiff formatting

:Gedit <file> (or :0 for current file) will edit the git indexed version of a file

Merge conflicts

Say you are trying to merge a feature into the master branch. 3-way vimdiff :Gdiffsplit or :Gvdiffsplit will arrange the panes as:

target branch | working copy | merge branch

and :Gdiffsplit3 or :Gvdiffsplit3 arrange panes:

target branch | common ancestor | merge branch
----------------------------------------------
                 working copy

Unlike 2 way diffs, 3-way diffs require you to specify a bufspec or buffer name to run the vim :diffput and :diffget commands on. You can list the buffers available with :ls, and use the numerical ID, or the buffer name. (or just a uniquely identifiable portion of the buffer name). You can also get straight to the vimdiff panes if you configure

git config [--global] diff.tool vimdiff

and then run

git mergetool

]c and [c to jump between conflicts. :dp shortcut to :diffput <target> that assumes target is working copy. :dg shortcut to :diffget.

Gdiff uses a quickfix buffer. To navigate this:

:cn(ext) open next file
:copen open file under cursor
:cnf open first file.

See Also

Fugitive.vim - resolving merge conflicts with vimdiff

Tagged: | git | vim |