Steve Grunwell

Open-source contributor, speaker, and electronics tinkerer

Quick-tip: git add –patch

I was sharing my screen with a co-worker the other day, and as I was committing code to a Git repository I commented “oh, I love git add -p.” I hear a confused “huh?” on the other end of the call, and I got to bust out my best (non-murderous) smug Patrick Bateman:

Patrick Bateman (American Psycho) being all smug

From the Git 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.

I first learned about git add -p in Sparkbox’s Atomic Commits with Git Foundry post, and I’ve been using it ever since. If you’ve ever found yourself in a situation where you’re working on multiple code changes at once (pro-tip: try to avoid this) and want to split them across multiple commits (or simply not commit parts of it), git add -p will let you specify separate parts of the file to be committed.

Previous

Review: Driftaway Coffee

Next

On Weight Loss

2 Comments

  1. Good little post, and a great little Git trick. I wish it were the default, similar to the way it works in “Darcs”.

    ;-)

  2. git diff –staged is a great command to follow git add -p as it allows you to examine the content you just added

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Be excellent to each other.