5

I'm using Emacs + it's in very many ways fabulous magit-mode to work with git. There is one aspect of my workflow though that I can't map to this, and thus this question.

Sometimes you have a commit which should be reworked into separate commits. I can interactively rebase my commits with magit, and declare my intend to edit said commit.

Now I have to switch over to git gui, select "ammend last commit" which shows me the staged changes, and unstage those parts I want to become a separate commit. Then I commit, stage the exclusions, and commit again - voila, I have a new commit within my history.

I have so far not found a way to accomplish this using Emacs. Any hints?

deets
  • 6,285
  • 29
  • 28
  • This is probably better asked on Emacs.SE. – Sean Allred Nov 12 '14 at 13:46
  • Hey, how do you interactively rebase commits ? :) – Ehvince Nov 12 '14 at 13:56
  • @SeanAllred Oh, I wasn't aware there is one.Go to the log ("l l" from the magit-info buffer), go to a commit, and press "E". Will i-rebase all commits from there on. Make sure you save the buffer with "C-x #" as it's a remote buffer. – deets Nov 12 '14 at 14:06
  • 1
    On the `next` branch rebasing has been improved a lot, e.g. the commits yet to be reapplied, as well as the already reapplied commits, are shown in the status buffer while a rebase is in progress. Your question ("how to split up a commit") I have answered in detail elsewhere: https://github.com/magit/magit/issues/966#issuecomment-60498587. (What Jordon suggests below is one way of doing it, but when using the `next` branch there is a more elegant variant). – tarsius Nov 12 '14 at 21:18

1 Answers1

6

After your begin the rebase, instead of going to the gui, do a magit-reset-head to HEAD~1, this will put you in the position to unstage the changes, and stage and commit as desired before finally Continuing the rebase from the magit status buffer.


Example scenario workflow:

  1. commit changes to file A (commit 1)
  2. commit changes to file B and file C (commit 2)
  3. commit changes to file D (commit 3)
  4. realize that you want the changes to file B and file C to be separate commits
  5. run magit-log
  6. begin an interactive rebase (E) at commit 2.
  7. specify you want to edit (e) commit 2 and finish (C-cC-c)
  8. return to magit status buffer.
  9. run magit-reset-head (x), specify HEAD~1 in prompt.
  10. you will see the changes for commit 2 staged.
  11. unstage the changes to file C.
  12. commit the staged changes for file B.
  13. stage and commit the changes to file C.
  14. continue and finish the rebase with RC

You will now have four commits, each changing one file in the order A, B, C, D.

Jordon Biondo
  • 3,974
  • 1
  • 27
  • 37