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

How do I stage and unstage Git files without leaving Vim?

Answer

:Git

Explanation

The vim-fugitive plugin by Tim Pope provides an interactive Git status window that lets you stage, unstage, diff, and commit files entirely from within Vim. Running :Git (or :G) with no arguments opens a summary buffer that replaces the need to switch to a terminal for everyday Git operations.

How it works

Running :Git opens a split showing:

  • Unstaged changes — modified files not yet added to the index
  • Staged changes — files ready to be committed
  • Unpushed/unpulled commits — ahead/behind status of your branch

Key mappings in the status buffer

Press g? at any time to see all available mappings. The most essential ones:

s     " Stage the file or hunk under the cursor
u     " Unstage the file or hunk under the cursor
-     " Toggle staging (stage if unstaged, unstage if staged)
=     " Toggle inline diff for the file under the cursor
dd    " Open a diff split for the file under the cursor
cc    " Create a commit (opens commit message editor)
ca    " Amend the last commit
cA    " Amend the last commit without editing the message
X     " Discard the change under the cursor (checkout/clean)
<CR>  " Open the file under the cursor

Staging workflow example

  1. Run :Git to open the status window
  2. Move your cursor to an unstaged file
  3. Press = to expand the inline diff and review changes
  4. Press s to stage the entire file, or visually select specific lines and press s to stage a partial hunk
  5. Press cc to start writing your commit message

Partial staging

One of fugitive's most powerful features is partial hunk staging. Expand a file's diff with =, then visually select specific lines within the diff and press s to stage only those lines — just like git add -p but with full visual control.

Tips

  • Press dv on a file to open a vertical diff split, where you can use :diffput and :diffget for granular staging
  • The status buffer auto-refreshes when you return to it, so you can edit files and come back
  • Use czz to stash changes and czA to apply the top stash
  • Press ri to start an interactive rebase directly from the status window

Next

How do I edit multiple lines at once using multiple cursors in Vim?