July 29, 2013
The Git Add Command's Update Option

The discovery of hunk staging in Git resulted in me spending some time looking through the other options available with Git’s add command.

The only option that jumped out at me was the update command (git add -u). The update command allows you to stage all tracked files for commit, but not any newly created, untracked files.

The situation that I found this command useful for was when deleting a lot of files or directories. The Terminal’s auto-complete shortcuts tend to fail when the file no longer exists, which means the remove command (git rm) can be a bit tedious. The add command’s update option allows you to stage all removed files at once.

It’s not as paradigm shifting as hunk staging, but the add command’s update option can certainly save you a bit of time in the right situation.

July 27, 2013
Git's Hunk / Patch Staging

“Yeah, I think they’re called Chunks,” one of my fellow developers said. I was in the process of moving our iOS frameworks from a Subversion repository over to individual Git repositories, the hierarchy of which I plan to share in a future post.

“Or maybe it’s Hunks,” he continued. We went on to have a lengthy conversation about this horribly named featured of Git. And, if you don’t think “hunks” is all that bad, try having a conversation about it in an office with an open layout. Let me know how many odd looks and raised eyebrows end up pointed your way.

Bad naming and colleague judgment aside, I’m glad this developer turned me on to hunks (see how easy it is for the conversation to get weird). Instead of staging a whole file with multiple changes for different purposes, hunk staging allows developers to choose which changes within the file to stage, and which to leave for later stagings.

To admit, after learning of hunks, I wasn’t convinced of its usefulness. Sure, I could see how it would allow for more granular, descriptive committing practices, which we should certainly strive for. Even with that – maybe due to the name itself, I’m not sure – but I didn’t expect hunks to become a part of my development process. I especially didn’t expect it to happen almost immediately. I certainly didn’t imagine that git add -p would soon become the default way that I stage anything and everything for committing. But, it did. And, it is.

By the way, that command is just how simple the feature is to use from the command line. It basically turns your terminal into a very simple code review application that allows you to make choices about what to stage.

The screen capture below gives you an idea of the presentation (click to expand). As you can see, there’s quite a few options, the main ones in my opinion being:

y - Oh yeah, stage that hunk.
n - Eew, gross. Get that hunk away from me.
s - Split that hunk. Yeah, just like that.
e - If you want a hunking done right, do it yourself.

As I mentioned already, I’m hooked on hunks. Adding a simple code review to the commit process, even if its only a review of your own code, is invaluable. Give it a try. I’m sure you’ll also appreciate the power of hunk staging.

Check out this documentation for more details on staging with patches and hunks.