Git tip - How to stage a hunk of code via the command line

The command line is my preferred way of using git. My code editor (Sublime Text) and command line app (iTerm 2) is everything that I need. Although I have some minor experience with GUI tools, I just don’t need them. A third opened app next to the two previously mentioned isn’t needed. The only feature that I missed from tools like Tower 2 or SourceTree was the ability to stage small hunks of code from the same file independently and create separate commits from them. Happy days! Wes Bos published a fantastic collection of git tips and tricks a few days ago, and one of them is the feature that I was always missing. Less talking, more coding!

Let’s say we have a file helpers.js full of helper functions (for brevity we’ll keep this example simple). This file is already committed, and we spot some obvious flaws in our functions. Try to figure it out by yourself :)

function timesTwo(num) {
  return num * 2;
}

function timesFive(num) {
  return num * 2;
}

function timesTen(num) {
  return num * 2;
}

Let’s fix it…

function timesTwo(num) {
  return num * 2;
}

function timesFive(num) {
  return num * 5;
}

function timesTen(num) {
  return num * 10;
}

That’s better! Now, we would like to store these changes as two separate commits (one per function). This is how to do it in SourceTree.

Staging a hunk of code in SourceTree

Time for the command line. The well known command git add with less well known flag -p (patch) comes in handy now. Let’s do it!

git add -p helpers.js

Staging a hunk of code in Command line

Wow, git what do you want from me now?! Let me help you!

In our case, git made the suggestion for us to keep the two functions as one piece of code ready to stage. This isn’t what we want to do. We need to split the current hunk into smaller hunks by typing s.

Split current hunk into smaller hunks in Git

Much better. Accept by pressing y followed by n to skip the next hunk and commit the change on the first function.

Commit separate independent hunks of code in Git

Now you are ready to follow the process yourself with the remaining functions. Hopefully this has helped you out.