I was using
git rebase to merge changesets from one subversion repository onto code from a completely different repository. Figuring out multiple svn-remotes and the attendant issues was fun1, though once I figured out I did want to
rebase, which onto what, how to delete a remote branch, and how to use
rebase --interactive to edit
git svn’s ported commit messages, it worked pretty well. But that’s not important.
During the rebase, I ended up off the branch I was trying to do the merge “in,” where
git branch said I was on
(no branch). However, like everyone else on the internet, I blithely continued past that point. How do I recover from
(no branch) without losing the completed rebase?
mpaschal@mpaschal-mt:~/svn/mt/git$ git branch -a * (no branch) atompub master git-svn github/master trunk
Of the search results I consulted, Mark Guzman best described what happened:
Little did I know that I had entered the “(no branch)” state. At this point I was pretty much gunning to create orphaned blobs, commits and other such items.
(no branch) is when
HEAD is a commit that is no longer one of the leading branch commits (which is what it means to be “on a branch”). If as in this case I want
atompub to match
HEAD, I have to
git merge the outstanding
HEAD commits over to
atompub. That works fine since the new commits are changesets atop
atompub in the first place; it’s as though I’m on a branch of
atompub, only I never explicitly branched, so it doesn’t have a name. Instead I have to refer to it by the commit ID.
As I hadn’t switched back to the real branch yet, I didn’t have to recover my nameless
HEAD commit the way Mark did. Instead I could look directly at the log:
mpaschal@mpaschal-mt:~/svn/mt/git$ git log -1 --pretty=oneline 658b8173ab396c7bb765f990c2bc2fdc7d639c86 Merged all ...
Then switch and merge:
mpaschal@mpaschal-mt:~/svn/mt/git$ git checkout atompub Switched to branch "atompub" mpaschal@mpaschal-mt:~/svn/mt/git$ git merge 658b817 Updating 461f2f7..658b817 Fast forward ...
It’s like magic!2
1 Not actually fun.
2 It’s a long incantation you have to research to discover, and getting it wrong can have disastrous consequences.