5

How do you stage a hunk into git from ediff?

I'd like to be able to view a side-by-side diff and stage hunks into the "HEAD" side of the diff, but that buffer (i.e., spacemacs.~master~) is read-only. Ideally, I could also edit that staged file (to achieve the precision I'd get from editing a hunk from git command-line).

It looks like I can invoke magit status (M-x magit-status), expand a diff (TAB), and use s to stage a diff hunk, but I'd like to do this on a file-by-file basis and from a side-by-side diff.

I'm hoping to replicate the behavior of vim-fugitive.

  • Related: Git: is it possible to stage a selected region directly from the file buffer?, but it looks like git-gutter cannot stage arbitrary regions. – idbrii May 13 '15 at 12:38
  • Related: Magit split hunk into two hunks, but it looks like magit cannot split hunks. – idbrii May 13 '15 at 12:39
  • 2
    you can split a hunk by moving point to a hunk in magit-status, select the relevant part you want to split and press s. magit then only stages the selected region and leaving the rest out. You can create a commit out of it then repeat for the remaining part. – Tu Do May 13 '15 at 13:09
  • I'm using spacemacs and when I open status, TAB to expand a diff, V to select a line in the hunk, and s to stage, I get evil-change: Buffer is read-only: #<buffer *magit: emacs.d*>. If I comment out my visual mode s binding and try again, then emacs is waiting for another key (I see s- at the bottom). Pressing S gives me the same read-only error. – idbrii May 13 '15 at 13:22
  • 1
    It is fixed in develop branch. See this issue. – Tu Do May 13 '15 at 13:30
8

Move point to the file you want to partially stage/unstage using Ediff and press e (magit-ediff-dwim). Alternatively you can also leave point where it is and press E s (magit-ediff-stage) but then you will be asked about the file in the minibuffer - so moving to the file first might be more convenient.

In the resulting Ediff session use c b to stage and a b to unstage. Then finish the session using q. You'll be asked whether you really want to finish the session. Then you'll also be asked whether you want to update the index with the contents of the buffer representing the index in Ediff. You do - the Ediff commands used previously only modified the buffer without modifying the actual index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.