Stage with confidence using git add -p

osuka42 profile image Oscar Mendoza ・1 min read

From the official documentation

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.

git add -p
git status -v

I use this all the time to avoid committing something unintended.
Try it!


Editor guide
lukad profile image
Luka Dornhecker

I am not an adherent of tiny commits at all.

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.

How is it better than git diff?

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 pressing e.
It's just a matter of preference though.

jess profile image
Jess Lee (she/her)

@maestromac showed this to me a little while ago and now I use it every time!

maestromac profile image
Mac Siri

Always add -p!

lukad profile image
Luka Dornhecker

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.

3sanket3 profile image
Sanket Patel

That's awesome. Thanks for sharing.