gitgit-rebase

accidentally created & changed to a new git branch while rebasing


I was mid-rebase, let's say on branch foo, and I accidentally ran git checkout -b net5, and somehow it proceeded successfully! (I intended to type this command into a different console window.)

Now I'm in branch net5, and I can't get back to foo. They're the same, sure, except foo was tracking a remote.

(It seems like a bug that git allowed this in the first place...)

3014 ~/fork/quickfixn$ git checkout foo
UnitTests/Logger/FileLogTests.cs: needs merge
UnitTests/SessionTest.cs: needs merge
error: you need to resolve your current index first

ARGH. I want to resolve the rebase on foo, not net5!

Anyway I can somehow undo this? I really don't want to resolve it on net5 and then merge it back to foo.


Solution

  • Disclaimer: This is not intuitive; I'm just reporting what I witnessed.

    I just tried this with the following steps:

    1. Start an interactive rebase on a branch called original-branch, and changed one of the commands to edit.
    2. When the rebase paused, I ran the command git checkout -b newbranch. This created and switched to a new branch as you described.
    3. Strangely, git status reported that I'm on newbranch, but my Git Bash command prompt shows that I'm still rebasing the original branch!
    /c/Git/TestRepo (original-branch|REBASE 2/2)
    $ git status
    On branch newbranch
    
    1. Just keep going! The rebase finishes.
    2. Now git status shows that my rebase succeeded and I'm still on original-branch.
    3. Surprisingly, git log shows that newbranch followed the rebase as well, and now both branches are pointing to the HEAD commit!

    So the answer to your question is, just keeping going and finish your rebase, and when you're done, simply run the command:

    git branch -d net5 # delete the unneeded branch

    And you're done!


    Side Note: Regarding this statement:

    Anyway I can somehow undo this? I really don't want to resolve it on net5 and then merge it back to foo.

    Even had you finished the rebase on net5, since branches are simply pointers to commits, you could have simply:

    # finish the rebase on net5
    git switch foo # checkout foo again
    git reset --hard net5 # point foo to the same commit net5 is pointing to
    git branch -d net5 # delete net5
    

    Then you would have been in the same place as above. It just turns out you don't even have to do that since Git appears to do what you wanted in the first place.


    Additional Note: this was tested using Git version 2.43.0.windows.1.