reflog ‐ switch to previous commit - renekuda38/git GitHub Wiki

Issue: Accidentally rename the branch

What happened?

I've accidentally renamed the feature branch to main (master).

$ git checkout -b feature # created new branch
$ git branch 
* feature
  main
$ git branch -M main # rename of feature branch to main
$ git branch        
* main # only this renamed branch will last, old main is deleted

Fix: Rebuild repos manually or fix via this solution

Solution

$ git branch state-now
$ git reflog
f296e56 (HEAD -> main, origin/main, origin/feature, origin/HEAD, state-now) HEAD@{0}: Branch: renamed refs/heads/feature to refs/heads/main
f296e56 (HEAD -> main, origin/main, origin/feature, origin/HEAD, state-now) HEAD@{2}: commit: dark side
1d02b56 HEAD@{3}: checkout: moving from main to feature
1d02b56 HEAD@{4}: merge feature: Fast-forward
ab3f9a8 HEAD@{5}: checkout: moving from feature to main
1d02b56 HEAD@{6}: commit: cw
ab3f9a8 HEAD@{7}: checkout: moving from main to feature
ab3f9a8 HEAD@{8}: clone: from https://github.com/renekuda38/test.gi
$ git checkout HEAD@{6}
$ git branch
* (HEAD detached at 1d02b56)
  main
  state-now
$ git branch -d main # delete the main, because it not contains what it should
$ git branch main # create a branch with current state
# because you are not on any branch but on detached HEAD
$ git checkout main
Switched to branch 'main'