Patch mode allows you to stage parts of a changed file, instead of the entire file. This allows you to make concise and well-crafted commits that make for an easier to read history. This feature can improve the quality of the commits. It also makes it easy to remove parts of the changes in a file that were only there for debugging purposes - prior to the commit without having to go back to the editor.
It allows you to see the changes (delta) to the code that you are trying to add, and lets you add them (or not) separately from each other using an interactive prompt. Here's how to use it:
git add -p
(will go through all modified files)git add -p path/to/file.c
(only look for changes in this file)
or you can use
git add -i
and choose type5
orp
(for patch).
Git will ask you which files you would like to partially stage; then, for each section of the selected files, it will display hunks of the file diff and ask if you would like to stage them, one by one:
At each point, you will be asked whether you want to "stage this hunk". Here are the commands you can use:
y
- stage this hunkn
- do not stage this hunka
- stage this and all the remaining hunks in the filed
- 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 regexj
- leave this hunk undecided, see next undecided hunkJ
- leave this hunk undecided, see next hunkk
- leave this hunk undecided, see previous undecided hunkK
- leave this hunk undecided, see previous hunks
- split the current hunk into smaller hunkse
- manually edit the current hunk?
- print help
- If git presents you with a chunk larger than what you would like to add, you
can use the
e
interactive command to specify the exact lines that you want added or removed. This is probably the most powerful option. As promised, it will open the hunk in a text editor and you can edit it to your hearts content. - Split the hunk into smaller hunks. This only works if there’s unchanged lines between the changes in the displayed hunk, so this wouldn’t have any effect in the example above.
git reset -p
works in a similar waygit commit -p
it combinesgit add -p
andgit commit
in one command.