Crafting the Perfect Git Commit

git add -p

You probably already use git for source control and are probably very familiar with git add.

Perhaps you haven’t yet discovered a very simple way to keep your commits small and meaningful…

Pass the -p or --patch option to git add and git will split your files into ‘hunks’ and present a diff for each hunk, one at a time with various options:

1
2
3
4
5
6
7
8
9
10
11
12
13
y - stage this hunk
n - do not stage this hunk
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

Using git add -p in this way allows you to create small, sensible commits that are very focused. Additionally, it gives you an opportunity to self-review your changes as you add, win-win!

Further reading

https://git-scm.com/book/en/v2/Git-Tools-Interactive-Staging#Staging-Patches

Gareth Talty
Senior Software Engineer