Git December 26, 2015 jess No comments

My favorite underutilized git option

We all strive for clean, single purpose commits with meaningful messages. This can be difficult in practice if you’ve done a lot of debugging since your last commit. Many people I know use git commit -A or git commit . when developing and maintaining features. This is fine when making small changes, but I don’t typically like to use these options when committing more than a line or two. When I suggest my favorite git add option instead, many people tell me that they’ve never heard of or used it.

the git add p flag (-p)

Rather than committing all changes in a file, git add --patch or -p allows you to stage changes in related hunks. It pops you into an interactive menu that allows granular control over staging changes. The hunks you see will be the same as the hunks that git diff prints out. As you see each change, you can choose to y to stage, n to skip, or s to separate out into even more granular pieces.

Using the interactive menu provides a few benefits. First, it allows me to review the code I intend to commit before staging it. I catch typos more often (though not as often as I should) when given the chance to glance through the changes. Second, if I’ve made a several changes to a file that belong to different trains of thought, I can stage and commit them separately as appropriate.


Say I’m starting a new project and I want a basic User class:

After I commit this code and I come back to it, I notice a few things I want to change.

First, I realize that I’ve spelled “first” as “fist” a number of times, and that needs to be corrected. Second, I realize that I don’t like having “role” be a string variable on my class. I would rather have different user roles be subclasses of User. So I change the file to look like this.

These are two separate thoughts I want to capture, so I should save these changes in separate commits. Using git add -p makes this easy even though the two separate thoughts occur in the same file.

When I type in git add -p I see the following.

Since my file is relatively short, it all appears in one hunk. I can separate this out by choosing the s option.

Now I have a single commit that handles fixing my typos. I can use git add -p again to review the changes I need for my next commit, or I can use git add since I know I’m staging all of the changes in the file. For the sake of finishing out the example, I’ll use git add -p again.

When I look back at my git log I see two separate, concise commits. If I decide I need to revert the commit that eliminates the User.role attribute and breaks out the Admin and Guest classes, I won’t lose the typo fixes in the process.

I use this option to stage all of my changes for commit, even if they are one or two lines. It helps me catch typos like fist vs. first. I never have to worry about mixing typo fixes or de-linting in with important development commits. It also helps me keep ideas separate, even if I developed those ideas at the same time.