6

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.

idbrii
  • 293
  • 1
  • 10
  • Related: [Git: is it possible to stage a selected region directly from the file buffer?](http://emacs.stackexchange.com/questions/6028/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](http://emacs.stackexchange.com/questions/6083/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: #`. 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](https://github.com/syl20bnr/spacemacs/issues/1462). – Tu Do May 13 '15 at 13:30
  • I just pushed a fix to the `next` branch. The index buffer is now always writable when using ediff. It sounds like you already figured out how to use ediff for staging in it was only this bug that kept you from doing it. In case that is not so and for the benefit of others who don't know how to do it, I'll provide an answer. – tarsius May 13 '15 at 14:23
  • Okay, there are a few additional rough edges... investigating. – tarsius May 13 '15 at 14:41
  • Should work properly now (don't forget to pull again). – tarsius May 13 '15 at 14:54

1 Answers1

9

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.

tarsius
  • 25,298
  • 4
  • 69
  • 109