Table of Contents

partially adding lines to commits

git gui

Right click the hunk of code you want to stage, and select “Stage Hunk for Commit”. You can also Stage or Unstage by line(s) using git gui.

command line

use the patch parameter to git add:

git add -p

or

git add --patch

You are presented with options to work with hunks of code. These options work as follows:

Stage this hunk [y,n,q,a,d,/,e,?]? 
y - stage this hunk
n - do not stage this hunk
q - quit, do not stage this hunk nor any of the remaining ones
a - stage this and all the remaining hunks in the file
d - do not stage this hunk nor any of the remaining hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help

If you want to jump to another area of the file, use 'g'. If you are presented with one “hunk” of code, but there are lines in between those that you want to add or remove, you can use 's' to “split” the changed lines into smaller hunks. The most powerful option is 'e', which opens the diff in an editor. You may then modify the lines any way you wish. If some lines show as + or -, but you don't want to stage them right now, simply change the + or - symbol to a blank space (the lines in a patch file need to start with a space, -, or +. Save the file and close your editor. If the patch fails, you are given an option to edit it again.