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
- Run
:Gitto open the status window - Move your cursor to an unstaged file
- Press
=to expand the inline diff and review changes - Press
sto stage the entire file, or visually select specific lines and presssto stage a partial hunk - Press
ccto 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
dvon a file to open a vertical diff split, where you can use:diffputand:diffgetfor granular staging - The status buffer auto-refreshes when you return to it, so you can edit files and come back
- Use
czzto stash changes andczAto apply the top stash - Press
rito start an interactive rebase directly from the status window