Splitting a hunk with magit
Asked Answered
P

3

83

I have 2 edits very close by (and therefore part of the same hunk), that I'd like to commit separately. Is it possible to split a hunk from within magit?

Plane answered 1/3, 2012 at 18:35 Comment(0)
H
129

You can set the mark correctly with Ctrl+Space (C-SPC) and magit will only commit the selected portion.

Hierarchize answered 1/3, 2012 at 18:37 Comment(5)
I always set the mark at the bottom of the region, and move point to the top in order to stage or unstage regions, as I've had issues (which I can't recall the specifics of) doing it the other way around in the past. YMMV.Idoux
Re: my previous comment, I recently read the reason for this, which is that when you un/stage the region, point must be within the region you are acting on. You can, of course, use C-x C-x to exchange point and mark if you had done it the other way around.Idoux
Does it behave differently when using transient-mark-mode? Because I always select top to bottom and never had a problem.Hierarchize
It wasn't enough for me to set the mark (C-SPC). I had to press C-SPC a second time to activate transient-mark-mode.Fibrous
to be clear: magit will only commit whatever is selected. It doesn't matter how did you select e.g., I use Shift + <arrows> to select portion of a hunk and press s to stage the selected part.Bab
I
62

You can also use - and + to decrease or increase the extent of the hunks in the diff (and 0 resets), but dominikh's answer is the most important one to know about.

Idoux answered 1/3, 2012 at 20:59 Comment(3)
This does not work for me, has this been removed in the newer version of magit or is this something peculiar to my setup?Iconostasis
Mike H-R: assuming you're in the magit-status buffer, then this should still work AFAIK.Idoux
Yes, I was mistaken, you need to press + or - on the line starting @@ (the hunk information line) rather than inside the hunk as I had been trying to.Iconostasis
H
0

Magit reuse diff-mode but don't expose many of diff-mode commands in its magit-revision-mode as this does VC package.

For example you can call diff-split-hunk which is usually C-c C-s in diff-mode.

Humidor answered 19/10, 2018 at 20:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.