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:
From the Git 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
patchsubcommand. 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.
Good little post, and a great little Git trick. I wish it were the default, similar to the way it works in “Darcs”.
git diff –staged is a great command to follow git add -p as it allows you to examine the content you just added