DEV Community

Oscar Mendoza
Oscar Mendoza

Posted on

16 3

Stage with confidence using git add -p

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.

git add -p
git status -v

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

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

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.

Collapse
 
jess profile image
Jess Lee

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

Collapse
 
maestromac profile image
Mac Siri

Always add -p!

Collapse
 
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.

Collapse
 
3sanket3 profile image
Sanket Patel

That's awesome. Thanks for sharing.

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay