Skip to main content

Git Better with Vim

Notes on Git Better with Vim by VimTricks.

Chapter 2: “Core git operations”

Requires tpope/vim-fugitive

  • :Gread - Reset the file to the last checked-in version
  • :Gread main:% - Reset the file to the version on main
  • :G commit - Create a commit and split open a new buffer to enter a commit message
  • :G commit -m "Message here" - Create a commit with a single-line message

Chapter 3: “Staging diffs with Vim”

From the :G window…

  • ) - go to next file or hunk (if inline file diff open)
  • ( - go to previous file or hunk (if inline file diff open)
  • v + movement(s) - select range from hunk to stage (no minimum range)
  • shift-V + j/k - select lines from hunk to stage (must select at least 2 lines)
  • = - toggle open or close the inline diff
  • - - toggle staging of a file or hunk
  • s - stage of a file or hunk
  • S - stage all files
  • u - unstage file or hunk
  • U - unstage all files
  • X - discard the changes to the hunk or file under your cursor (be careful!)

Chapter 4: “Visually staging diffs”

  • :G + dd on a file - view diff with working tree on the right/bottom (:Gdiffsplit)
    • split will be vertical or horizontal depending on space
    • force vertical with dv instead
  • :Gdiffsplit from within the file itself - view diff with working tree on the right
    • force a vertical diff with :Gvdiffsplit
  • In the two-way diff…
    • ]c - jump to the next change
    • [c - jump to the previous change
    • do - “obtain” the corresponding change from the opposite window
      • in working tree window, this discards the change
    • dp - “put” the change under the cursor into the opposite window
      • in working tree window, this keeps the change
    • :wq - save and close the diff windows to stage the resulting change

Chapter 5: “Committing”

From the :G window…

  • cvc - commit while viewing the diff I’m committing in the commit window
  • ca - amend the last commit and edit the message
  • ce - amend the last commit with the “existing” message
  • cw - change the wording of the last commit
  • c-space - populate the command line with :Git commit so I can construct a custom command

Chapter 12: “Conflict resolution”

From the :G window when there is a merge conflict…

  • dd/dv - will open to a 3-way split:
    • left/top (buffer 2) - the currently checked out branch (”ours”)
    • middle (buffer 1) - the working version where the resolved conflicts go
    • right/bottom (buffer 3) - the branch I’m merging into the current branch (”theirs”)

From the 3-way split view…

  • To keep everything from the left/top or right/bottom version:
    • Move cursor to the buffer with the changes you want to keep
    • Run :Gwrite!
  • To select changes from both sides…
    • Stay in the middle window (buffer 1)
    • ]c - jump to the next conflict
    • [c - jump to the previous conflict
  • When the cursor is on a conflict…
    • d2o - “obtain” the version from buffer 2 (”diff two obtain”)
    • d3o - “obtain” the version from buffer 3 (”diff three obtain”)

Chapter 18: “Connecting Vim to the Web”

Requires X plugin

  • :GBrowse - open the file or selected lines in the browser
  • :GBrowse! - copy a link to the file or selected lines
  • :GBrowse master...your_branch - open diff between two branches in the browser
  • :GBrowse some_commit...another_commit - open diff between any two commits in the browser (same as above)

Chapter 19: “History Visualization”

Requires X plugin

  • :GV - list previous commits to current branch in new tab
    • useful for PR re-reviews?
  • :GV! - list previous commits to current file in new tab
  • From the GV/ GV! tab…
    • o or <cr> - open the selected commits in the right pane
    • O - open the selected commits in a new tab
    • gb - open commit under cursor in the browser
    • From the commit detail window…
      • ]n - jump to next hunk
      • [n - jump to previous hunk