ChatGPT解决这个技术问题 Extra ChatGPT

Splitting a hunk with magit

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?


F
Flow

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


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.
p
phils

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.


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.
g
gavenkoa

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.


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now