Skip to main content

Splitting a hunk in Git’s patch mode

Posted in Development and Git

When you enter Git’s patch mode, the chunks of code (‘hunks’) you’re offered to stage/skip can sometimes be bigger than you’d want. Maybe a hunk you’re offered contains multiple lines with changes that belong in more than one commit. Luckily, the s option is there to split the hunk down further.

Let’s say we have a file that had 3 lines:

  1. Line 1
  2. Line 2
  3. Line 3

If we forgot to add a space between the word ‘Line’ and its number for lines 1 and 3, we’d go back to fix it; our the diff when we enter patch mode would look like this:

-Line1
+Line 1
Line 2
-Line3
+Line 3

Because the second line is unchanged, the s option will break that hunk down into two smaller hunks, one for Line 1 and the other for Line 3.

If, however, we forgot to add spaces between the word and the number on every line, patch mode wouldn’t offer the s option as we’re unable split the hunk down further. The reason for this is that there are no unchanged lines between those we are changing:

-Line1
+Line 1
-Line2
+Line 2
-Line3
+Line 3

Accessibility in your inbox

I send an accessibility-centric newsletter on the last day of every month, containing:

  • A roundup of the articles I’ve posted
  • A hot pick from my archives
  • Some interesting posts from around the web

I don’t collect any data on when, where or if people open the emails I send them. Your email will only be used to send you newsletters and will never be passed on. You can unsubscribe at any time.

More posts

Here are a couple more posts for you to enjoy. If that’s not enough, have a look at the full list.

  1. Upgrading from iPhone 13 mini to 16 Pro

    I get a new phone every 3-ish years, give mine to my wife, and now she gives hers to our daughter. I got a 16 Pro this year! Here’s the skinny.

  2. Apple, you’re doing the Dynamic Island wrong

    I love the idea of Dynamic Island; making lemons into lemonade and all that. But, in my opinion, Apple have got the fundamentals mixed up.