gitrebasegit-interactive-rebase

git interactive rebase: stop without commit


(TLDR) Is there a way in git rebase -i to stop for editing, without a commit id? (/TLDR)

Longer version:

Background

With git rebase -i, I get a text editor where I can define a list of commands, starting with pick COMMIT_ID on each line.

One of the options is to replace "pick COMMIT_ID" with "edit COMMIT_ID", which means it will stop after the commit for me to do amend the commit, or to do some manual operations. Then I can proceed with git rebase --continue.

From the list of options:

#  e, edit = use commit, but stop for amending

Question

I wonder if there is an option to stop for editing, without picking any commit.

Motivation / Use case

E.g. if I want to squash a range of commits, and stop afterwards for some manual operations, I would have to put edit + squash in front of the same commit id - which is not allowed.

Instead, I would do something like this:

pick COMMIT_0 Change some colors
pick COMMIT_1 Make it faster
squash COMMIT_2 Fix a typo in previous commit
squash COMMIT_3 Fix another typo in previous commit
edit
pick COMMIT_4 Refactor some things
pick COMMIT_5 Introduce new options

Such an option would also allow to stop before the first commit in the sequence.

(I was going to say it would allow to edit before the initial commit of the entire history, but this is not true - the initial commit is never part of the rebase sequence)

If I do it like this, git says this:

Warning: the SHA-1 is missing or isn't a commit in the following line:

Interestingly, the effect is more or less what I want, it stops and I can do things. But I am sure this is not the intended way to do it.


Solution

  • You have one, two, or three options, depending on some circumstances.

    First option

    Insert

    exec false
    

    or its short form

    x false
    

    at the point where you want to stop. As you may know, exec runs the shell command at the point where the command occurs; but if the command exits with failure, git rebase stops, and the command false always exits with failure.

    Second option

    If you are using Git 2.20 or later, you can insert

    break
    

    or its short form

    b
    

    at the point where you want to stop. This command was introduced for the very purpose that you are asking for.

    Third option

    Mark the commit before the point where you want to stop with edit instead of pick. Obviously, this does not work if you want to stop before the first commit or after a squash or fixup commit.