Interactive Git Commits

Today (as with many times before), I was reviewing some code I had changed and realized, “I want some of these lines but don’t want these other lines in this commit.” Enter git add -p. This starts git addition in interactive mode, so that you can select what you want and what you don’t.

alt

With each change, I can choose what to do with it - I can accept it (pressing y which will stage it for the commit, hit n to not stage it, or s to split it). If the split one isn’t good enough, you can hit e to edit. This opens up your favorite editor with the patch (and with a small instruction list at the bottom).

alt

After this process, if you do a git status, you’ll see that some of the changes are already staged for commit (you can do a git diff --cached to see those), and others are left un-staged.