From the official documentation
-p
--patch
Interactively choose hunks of patch between the index and the work tree and add them to the index. This gives the user a chance to review the difference before adding modified contents to the index.
This effectively runs add --interactive, but bypasses the initial command menu and directly jumps to the patch subcommand. See “Interactive mode” for details.
Said so, git add -p
allows you to pick which chunks of code are going to be staged from all your current changes.
I use this all the time to avoid committing something unintended.
Try it!
Top comments (5)
I'm not adherent of tiny commits either but I like to keep my commits clean and have them not change too many things that are not related.
For example when I work on a feature and I do some refactoring unrelated to that, I like to keep those changes separate.
You can discard (delete) changes that you don't want to commit by pressing
d
and you can edit the diff you are currently viewing by pressinge
.It's just a matter of preference though.
@maestromac showed this to me a little while ago and now I use it every time!
Always
add -p
!I often use
add -p
to split my changes into several smaller commits that make sense on their own instead of creating one giant commit.I also use it to review my changes before committing.
That's awesome. Thanks for sharing.