Home > Mobile >  I used "git branch -M main" inside a branch, and my original "main" disappeared
I used "git branch -M main" inside a branch, and my original "main" disappeared

Time:07-26

I ran into a little predicament while trying to push my local repo to remote (on GitHub)...

So, I had my main branch and was developing in a second one, call it second-branch

I wanted to push my code to GitHub now. So I created a repo online and followed their own instructions which is:

github instructions

So I copy and pasted these commands, from inside second-branch

What happened is that second-branch is now renamed main...

And the original main is nowhere to be found. I run git branch and it returns only main, but the content of it it's what before was the content of second-branch

I don't know where to look for my original main, so any help is really appreciated

Can only pray that it's not lost lol

Thanking in advance for any consideration

CodePudding user response:

You can try the command:

git reflog

What it does is listing every instance in which a ref was updated. You should see something similar to this:

eff544f HEAD@{0}: commit: migrate existing content
bf871fd HEAD@{1}: commit: Add Git Reflog outline
9a4491f HEAD@{2}: checkout: moving from main to git_reflog
9a4491f HEAD@{3}: checkout: moving from Git_Config to main
39b159a HEAD@{4}: commit: expand on git context 
9b3aa71 HEAD@{5}: commit: more color clarification
f34388b HEAD@{6}: commit: expand on color support 
9962aed HEAD@{7}: commit: a git editor -> the Git editor

The first column indicates the hash of the commit.

Try to find the most recent commit of your old branch, based on the messages you see in the last column.

Once you find it, take its hash value (first column). Then:

  • git checkout <hash>
  • git switch -c <new-branch-name>

You should now have your old master branch on this <new-branch-name>

  • Related