Git: cancel an interactive rebase

20,852

Solution 1

You can make vim exit with a non-zero exit code to cancel the rebase by typing :cq. The result of this is:

$ git rebase -i HEAD~4
# enter vim, then type :cq<enter>
Could not execute editor

From the vim help page we see that cq is short for cquit:

                                                        *:cq* *:cquit*
:cq[uit][!]             Quit Vim with an error code, so that the compiler
                        will not compile the same file again.
                        WARNING: All changes in files are lost!  Also when the
                        [!] is not used.  It works like ":qall!" |:qall|,
                        except that Vim returns a non-zero exit code.

Solution 2

If you're in the initial interactive rebase editor window that looks like this:

pick f7f3f6d changed my name a bit
pick 310154e updated README formatting and added blame
pick a5f4a0d added cat-file

# Rebase 710f0f8..a5f4a0d onto 710f0f8
...
...
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.

you can abort the rebase by deleting the entire contents of the editor window and saving it, or causing the editor to close with an error code.

In vim this can be accomplished with d SHIFT+g followed by :wq, or alternatively causing the editor to exit with an error as Mike H-R pointed out out using :cq.

If you've already left that window and are proceeding through the following squash/edits/etc, than this will not abort the entire rebase, but only the changes to the commit that is currently being edited. At this point you can kill the editor as above, but additionally you will then have to perform a git rebase --abort on the command line as well.

Share:
20,852
Meltemi
Author by

Meltemi

Updated on April 28, 2020

Comments

  • Meltemi
    Meltemi almost 4 years

    I like to git rebase -i HEAD~5 to squash my commits down. Sometimes I think I need to go back 5 commits but then realize I need 7. However git has already brought up the rebase editor .git/rebase-merge/git-rebase-todo in vim.

    If I quit that buffer (vim) then git says:

    Successfully rebased and updated refs/heads/my-branchname
    

    Is there a way to stop the rebase from w/in the editor? I suppose I could git rebase --abort from another shell but not sure what that would do to the git-rebase-todo file that I'd be in the middle of editing in another buffer.

    • Mike H-R
      Mike H-R over 8 years
      If you haven't changed anything inside the file then even though the branch was "Successfully rebased" all the commits are the same so it's effectively a no-op.
    • Mr Redstoner
      Mr Redstoner over 4 years
      @MikeH-R Not necessarily a no-op. I've done that and it generated a bunch of differences against origin. Because rebasing removed the merge commit that the branch had. Your :cq will not do that, causing an actual no-op.
    • jdhao
      jdhao almost 3 years
  • Meltemi
    Meltemi over 8 years
    OK, I'll bite; what is :cq?
  • Mike H-R
    Mike H-R over 8 years
    @Meltemi I updated my answer with the explanation of cq.
  • fourpastmidnight
    fourpastmidnight almost 8 years
    That is not correct. If you want to abort the entire rebase operation, but you're in the last squash of a series of interactive rebasing, this does not "abort" the interactive rebase. It only aborts the current rebase step commit.
  • whaleberg
    whaleberg almost 8 years
    @fourpastmidnight I'm not quite sure I follow. Deleting and saving only applies to the initial editor window where you choose to pick/squash/edit/etc. If you've already left that and are in the editor for an individual squash resolution then this doesn't apply. Is that the case you're talking about?
  • fourpastmidnight
    fourpastmidnight almost 8 years
    Yes, that's exactly right. I understood the OP's question to be about aborting the entire rebase process, but only after the OP was in the editor window for a squash, for example. I found myself in a similar situation yesterday and tried what you suggested in your answer. Unfortunately, it did behave the way you just described in your comment. Perhaps clarifying your answer will help others avoid my mistake (I'm a bit new to interactive rebase, myself). In any event, I was able to rebase again and edit/split the commit, but the behavior was unexpected.
  • fourpastmidnight
    fourpastmidnight almost 8 years
    Instead of saying "it will abort the rebase", perhaps say "It will cause the current rebase commit to be aborted." That makes it more clear what's really happening. Do you know of a way to abort the entire rebase once you're in the editor window? Does the vim :cq "trick" actually work from the answer above?
  • whaleberg
    whaleberg almost 8 years
    @fourpastmidnight I've reworded it and added more information. Hopefully that help avoid confusion in the future. The :cq trick is equivalent to deleting and saving as far as I can tell for this particular case.
  • fourpastmidnight
    fourpastmidnight almost 8 years
    Ah, ok. Thanks! I hope these edits will help other rebase --interactive noobs avoid my mistake.