82

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?

deepakg
  • 1,640
  • 1
  • 12
  • 10

3 Answers3

128

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

Flow
  • 22,048
  • 13
  • 91
  • 147
Dominik Honnef
  • 16,164
  • 7
  • 39
  • 43
  • 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. – phils Mar 01 '12 at 20:57
  • 2
    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. – phils Apr 08 '12 at 00:49
  • Does it behave differently when using transient-mark-mode? Because I always select top to bottom and never had a problem. – Dominik Honnef Apr 08 '12 at 12:56
  • 3
    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`. – Vebjorn Ljosa Nov 06 '14 at 03:04
  • 1
    to be clear: magit will only commit whatever is selected. It doesn't matter *how* did you select e.g., I use `Shift + ` to select portion of a hunk and press `s` to stage the selected part. – jfs Feb 15 '15 at 23:02
61

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.

phils
  • 66,286
  • 9
  • 132
  • 174
  • 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 Oct 27 '15 at 10:58
  • Mike H-R: assuming you're in the magit-status buffer, then this should still work AFAIK. – phils Oct 27 '15 at 12:37
  • 1
    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. – Mike H-R Oct 27 '15 at 15:01
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.

gavenkoa
  • 37,355
  • 13
  • 206
  • 248