Question

Accidentally deleted remote branch while trying to rename my master branch to main. Is there any way to restore it?

Recently, while trying to rename my master branch to my main branch on my remote github repo, I mistakenly followed a tutorial. I didn't read the tutorial thoroughly, and ended up deleting the master branch entirely, without any of the changes ending up on the main branch on the remote.

The commands I ran were the following:


        git branch -m master main //renames

        git fetch -p origin
        git branch -u origin/master main 

        git remote set-head origin -a 

        git branch -D master
        git push origin :master 

Is there any way to undo the last command to delete the remote branch. Unfortunately, I can't see any evidence of the master branch from github? I can only see that the master branch has been deleted in the git command line. I have changes on the remote branch where I directly edited the README file, but I never edited the README locally, so I can't restore it locally I think. Is there any way I can restore the branch remotely through Github?

I tried running git checkout -b master origin/master to get back any remainings of the origin/master branch, but it seemed to not work. I also tried git reflog to see if I can identify any README commits, but unfortunately I can only see commits related to my local changes.

Unfortunately, I made README changes on the remote without doing it on local for the easier UI experience on Github. I can restore the local changes, but I spent quite a bit on the README, so I would love if I could restore that as well.

 3  44  3
1 Jan 1970

Solution

 1

You're right about the reflog, and well done for trying it. As you rightly say, the commit in question is not there, because it's not a place where you ever were locally, if you see what I mean.

However, I don't think you have anything to worry about. The good news is that you said git fetch. That means that everything that the remote had at that moment, you had at that moment. This means that you have the commit that is characterized by the "changes on the remote branch where I directly edited the README file"!

You will have this commit for a while, so there is no big rush to find it, but eventually it will be deleted, so there is some rush to find it.

Here's what to do:

  • First, say git fsck --lost-found and the commits and blobs will copied to a safe place where you can explore them.
  • Second, explore them! You can do this by saying ls -al .git/lost-found/commit. You will see a list of commits, by SHA.
  • Third, examine each of those, by saying git show <sha>. You will quickly see which one is the README edit from the server. (It is possible to automate this examination, as shown for example in Can I get git fsck to show commit names?, but you probably don't have enough commits to make that necessary.)

Now you should be able to work with that commit; for example, you could cherry pick it onto the end of your main, or you could just use git show to extract the README.md file out of it.

2024-07-23
matt