Skip to content

Software Engineer at Heydoc

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!

  • y - stage this hunk
  • n - do not stage this hunk
  • q - quit; do not stage this hunk nor any of the remaining ones
  • a - stage this hunk and all later hunks in the file
  • d - do not stage this hunk nor any of the later hunks in the file
  • / - search for a hunk matching the given regex
  • s - split the current hunk into smaller hunks
  • e - manually edit the current hunk
  • ? - print help

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.

Comments

  • P
    Piotr Kowalski

    Thanks for the article. Glad to see that someone cares about good quality of revisions.

    👆 you can use Markdown here

    Your comment is awaiting moderation. Thanks!
    • Pawel Grzybek
      Pawel Grzybek

      Glad you liked the article. Thanks a ton!

      👆 you can use Markdown here

      Your comment is awaiting moderation. Thanks!
  • p
    pandammonium

    So that's how you do it! Thanks

    👆 you can use Markdown here

    Your comment is awaiting moderation. Thanks!
  • M
    MarceliJankowski

    Short, simple yet effective! I hate when I just want to get a tiny bit of information (like in this case where I just needed to see how to use this one) and the whole post takes 10 pages filled with redundant fluff. So thanks again!

    👆 you can use Markdown here

    Your comment is awaiting moderation. Thanks!
    • Pawel Grzybek
      Pawel Grzybek

      I am glad you found it useful!

      👆 you can use Markdown here

      Your comment is awaiting moderation. Thanks!
  • M
    Mike

    Hi Pawel, I really like your explaination. I have some questions to ask, if you don't mind so I get better clarity on the main purpose of hunk and how professionals use it.

    Please let me know is I am right or wrong.

    Basically hunk can be use to throw out added codes that do not reflect a change in the file and just show those lines of codes that have either changed or was added is that right ?

    Also if I am right, then hunk makes a pull request more easy to review as you can go straight to the point on the file changes of that pull request , am I right or wrong.

    I would be glad, If you are kind enough to through more light into this.

    Thank you 💓

    👆 you can use Markdown here

    Your comment is awaiting moderation. Thanks!
    • Pawel Grzybek
      Pawel Grzybek

      Hi Mike.

      Your understanding is correct. In the same way how you can select subset of files to the commit, sometimes you need the same level of granularity in an individual file.

      Thanks for visiting. Have a lovely day Mike 👋

      👆 you can use Markdown here

      Your comment is awaiting moderation. Thanks!

Leave a comment

👆 you can use Markdown here

Your comment is awaiting moderation. Thanks!