Interactive rebase "edit" to split commits

582 views
Skip to first unread message

Paul Smith

unread,
Nov 30, 2017, 2:54:48 PM11/30/17
to ma...@googlegroups.com
Hi all. This mailing list seems somewhat sparsely used but I don't
know that a GitHub issue is the right thing.

Fairly often I find myself wanting to split commits when I do
interactive rebasing and I'm wondering if anyone has hints for how to
handle this more easily using Magit.

What I do today is invoke interactive rebase from Magit and mark the
commit I want to split with "e" (edit). When Magit stops, I open a
shell and run git reset HEAD~, then I go back to Magit and use it to
add the bits I want to make the new commit(s). Often I want to use
some form of the original commit message when I create the extra
commits, so I also have to go spelunking to get a copy of the "under
edit" commit message and paste it in.

Then I always forget whether or not I have to create the final commit
myself, or whether "rebase continue" will do it for me (it doesn't :)).
Then I use "r r" to continue.

Are there Magit features that will make this simpler for me, that I
just don't know about? Things that would be nice:

* Automatically do the reset operation. I can't come up with the
right Magit "reset" operation for this. Is there a simple way to
run the "git reset HEAD~" operation, or a shortcut? Could there be
a mode in interactive rebase that would to this, some kind of
"split" operation or something instead of just "edit"? I guess that
requires a new feature in Git itself.
* Automatically insert the commit message from the commit I'm editing,
when I make a new commit. Or some command that will let me easily
yank that message and start editing it.
* Maybe something that does something smart if I "r r" (continue) when
there are still files modified or staged. I'm not sure about this
one.

I'm happy to create GitHub issues for these if appropriate.

Ben Schmeckpeper

unread,
Nov 30, 2017, 3:15:42 PM11/30/17
to pa...@mad-scientist.net, ma...@googlegroups.com
I can't help with the more complicated parts of your question, but you can run a reset from Magit with x (you'll be prompted for a commit to reset to, so you'll have to type HEAD~).  x runs git reset --mixed which is git's default mode for resets.  If you want to run with a different mode, X will give you pop up where you can choose the mode.


-Ben

--
You received this message because you are subscribed to the Google Groups "magit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to magit+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kyle Meyer

unread,
Nov 30, 2017, 5:00:59 PM11/30/17
to pa...@mad-scientist.net, ma...@googlegroups.com, Ben Schmeckpeper
Paul Smith <pa...@mad-scientist.net> writes:

> Fairly often I find myself wanting to split commits when I do
> interactive rebasing and I'm wondering if anyone has hints for how to
> handle this more easily using Magit.
>
> What I do today is invoke interactive rebase from Magit and mark the
> commit I want to split with "e" (edit).

If the only operation you want is split one commit, you can also call
magit-rebase-edit (r m), which lets you avoid the git-rebase-todo step.

> When Magit stops, I open a shell and run git reset HEAD~, then I go
> back to Magit and use it to add the bits I want to make the new
> commit(s).

As Ben mentions, 'x' then "HEAD~" gets you to the same place.

> Often I want to use some form of the original commit message when I
> create the extra commits, so I also have to go spelunking to get a
> copy of the "under edit" commit message and paste it in.

After a reset operation, ORIG_HEAD points to the commit you just left.
From the commit popup, you reuse its message by hitting '=C' and
selecting ORIG_HEAD from the completion candidates.

That won't give you a chance to edit the commit message. If you want to
do that, start the commit as usual ('c c') and then call
git-commit-prev-message ('M p').

> Are there Magit features that will make this simpler for me, that I
> just don't know about?

Hopefully the one I suggested do.

> Things that would be nice:
>
> * Automatically do the reset operation. I can't come up with the
> right Magit "reset" operation for this. Is there a simple way to
> run the "git reset HEAD~" operation, or a shortcut? Could there be
> a mode in interactive rebase that would to this, some kind of
> "split" operation or something instead of just "edit"? I guess that
> requires a new feature in Git itself.

I like the idea of wrapping the rebase and reset operations into one
command. I think it's a common operation, and I personally would use it
a lot. It's worth a feature request on GitHub to see what others think.

> * Automatically insert the commit message from the commit I'm editing,
> when I make a new commit. Or some command that will let me easily
> yank that message and start editing it.

Magit already has this covered with =C and git-commit-prev-message, I
think.

> * Maybe something that does something smart if I "r r" (continue) when
> there are still files modified or staged. I'm not sure about this
> one.

I'm not sure about that one either :)

--
Kyle
Reply all
Reply to author
Forward
0 new messages