Skip to content

Committing Partial Changes

Michael Goerz edited this page Dec 30, 2019 · 1 revision

One of the principles of "good" git commits is that they should be "atomic", i.e., only address one particular change or issue. This often makes it necessary to only commit parts of currently changed file.

On the command line, this is done with git add -p (short for git add --patch). Unfortunately, this can be a bit difficult, mostly because the "hunks" are presented out of context.

Stack Overflow discusses some alternatives, but as a vim user, the best way to stage files partially is to use the vim-fugitive plugin. This plugin provides the :Gdiff command that opens the staged version and the working directory version of the current file side by side. You can edit either file freely (mostly, deleting lines from the staged version with dd or yanking lines from the working directory version (Y) and pasting them into the staged version (p).

You may want to define some mappings in your vimrc to make vim-fugitive more accessible:

" Fugitive mappings
nnoremap <Leader>gd :Gdiff<Enter>
nnoremap <Leader>gD :Gdiff HEAD<Enter>
nnoremap <Leader>gs :Gstatus<Enter>
nnoremap <Leader>ga :Gwrite<Enter>
nnoremap <Leader>gc :Gcommit<Enter>
nnoremap <Leader>gb :Gblame<Enter>