Skip to main content
5 of 5
deleted 38 characters in body

Magit split hunk into two hunks

There's a question on SO about splitting hunks using magit, and the two solutions given are to either stage the region (mark a region, hit stage), or to shrink/grow all hunks using + & -. That's not what I'm after.

In magit's status buffer, I want to split a hunk into two hunks, at the point, or at least at the lines around the point.

Turning this (excuse my bad diff formatting)

@@ blah blah blah
- foo
+ bar
+ baz
+

Into

@@ blah blah
- foo
+ bar

@ blah blah
+ baz

When the point is on bar.

Motivations:

  • baz being a debug statement, so I'd like to drop it from the commit of foo & bar without getting rid of it.

  • bar and baz being only partially related, meaning that their changes should not be in the same commit.

  • bar is large and baz is small, making it a lot easier to split the hunk rather than select bar

  • Similar to the last point, say a 20 line commit contains a single line the middle that should not be staged. It'd be easier to split & stage the top and bottom two hunks ignoring the middle one than use the region.

  • The above can arise sometimes when the diff presentation will separate the before and after of something that's been modified, and have something unhelpful in the middle. e.g.

@@ line
- old_foo
+ random stuff
+ new foo
Squidly
  • 1.5k
  • 14
  • 19