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

How to stage individual Git hunks without leaving Neovim using gitsigns.nvim?

Answer

:Gitsigns stage_hunk

Explanation

gitsigns.nvim integrates git diff information directly into Neovim's sign column and provides commands to stage, unstage, reset, and preview individual hunks — all without opening a terminal. This enables surgical commit preparation: stage only the specific lines you want, leaving unrelated changes unstaged.

How it works

After installing and configuring gitsigns.nvim, these commands become available:

  • :Gitsigns stage_hunk — stage the hunk under the cursor (default mapping: <leader>hs)
  • :Gitsigns undo_stage_hunk — unstage the most recently staged hunk (<leader>hu)
  • :Gitsigns reset_hunk — discard the current hunk's changes (<leader>hr)
  • :Gitsigns preview_hunk — show the diff inline in a floating window (<leader>hp)
  • ]c / [c — navigate to the next/previous changed hunk

Example

Suppose you have two unrelated changes in the same file: a bug fix in the middle and a refactoring near the top. Navigate to the bug fix with ]c, confirm it with :Gitsigns preview_hunk, then stage it alone with :Gitsigns stage_hunk. The refactoring stays unstaged — letting you commit a clean, focused fix.

:Gitsigns preview_hunk   ← verify the diff looks correct
:Gitsigns stage_hunk     ← stage only this hunk
:Git commit              ← commit via fugitive (or :terminal git commit)

Tips

  • In Visual mode, :'<,'>Gitsigns stage_hunk stages only the selected lines within a hunk — useful when a hunk is large and you want to commit just part of it
  • :Gitsigns blame_line shows an inline blame annotation for the current line
  • :Gitsigns diffthis opens a two-pane diff for the whole file
  • Pair with vim-fugitive for a complete workflow: stage hunks with gitsigns, write the commit message with :Git commit

Next

How do I enable matchit so % jumps between if/else/end style pairs?