84

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?

3 Answers 3

131

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
Does it behave differently when using transient-mark-mode? Because I always select top to bottom and never had a problem.
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.
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.
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.

3 Comments

This does not work for me, has this been removed in the newer version of magit or is this something peculiar to my setup?
Mike H-R: assuming you're in the magit-status buffer, then this should still work AFAIK.
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.
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.