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.

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

Wow, git what do you want from me now?! Let me help you!
y
- stage this hunkn
- do not stage this hunkq
- quit; do not stage this hunk nor any of the remaining onesa
- stage this hunk and all later hunks in the filed
- do not stage this hunk nor any of the later hunks in the file/
- search for a hunk matching the given regexs
- split the current hunk into smaller hunkse
- 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
.

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

Now you are ready to follow the process yourself with the remaining functions. Hopefully this has helped you out.
Thanks for the article. Glad to see that someone cares about good quality of revisions.
Glad you liked the article. Thanks a ton!
So that's how you do it! Thanks
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!
I am glad you found it useful!
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 💓
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 👋