DEV Community

Oscar Mendoza
Oscar Mendoza

Posted on

Stage with confidence using git add -p

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!

Top comments (5)

lukad profile image
Luka Dornhecker • Edited

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

@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 • Edited

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.