Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: cancel an interactive rebase

Tags:

git

vim

rebase

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.

like image 383
Meltemi Avatar asked Nov 06 '15 01:11

Meltemi


People also ask

How do I cancel an interactive rebase?

To do this, simply delete all commits and actions (i.e. all lines not starting with the # sign) and the rebase will be aborted!

How do you finish a rebase?

When you're finished making all your changes, you can run git rebase --continue . As before, Git is showing the commit message for you to edit. You can change the text ( "i cant' typ goods" ), save the file, and close the editor. Git will finish the rebase and return you to the terminal.

How do I stop rebasing in Vscode?

Simply clearing the contents and saving the rebase that opens up in VS Code during the interactive rebase will abort the rebase as noted in its comments: # However, if you remove everything, the rebase will be aborted.


2 Answers

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. 
like image 114
Mike H-R Avatar answered Sep 22 '22 03:09

Mike H-R


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.

like image 35
whaleberg Avatar answered Sep 23 '22 03:09

whaleberg