Error: refname refs/heads/master not found


I am trying to publish my third site, so I fairly new to github, and I am getting: the following error:

$ git branch -M main
error: refname refs/heads/master not found
fatal: Branch rename failed

I have used git-bash, them some code on github account, and on the above stage I got the error.
I am bit stuck, I have search for the error, but I could not fix it.

Did you solve the problem ? I take that error too and couldn’t find solution

Could the branch possibly be named main already? Github changed the default name for the default branch from master to main a while ago.


Can I post the link to the solution here? Its an external source… and its in Portuguese:

Basically what I did was to run the

git fetch
git reset --hard origin/master

them git push

I had this error as well, but it cleared after I made my initial commit. That might be the issue?


I deleted the repo, and started all over again, and it come right. thanks :grinning:

same here, it turns out that to make a first push you gotta make a first commit first


I also deleted the repo and it worked!

I may be wrong but I believe that since a branch is a pointer to a commit, if no commit has been created yet, then no branch exists. So you cannot rename a branch which does not exist. The solution would be to do a first commit, which will create a branch (master by default), and then you should be able to rename it (now that it exists).



git config --global “
git config --global “Your Name”

and then make your first commit.

$ git commit -m “first commit”

and now run the command

$ git branch -M main

It worked for me this way.


Yeah, this here is the simpler solution.

1 Like

git init
git config --global ‘your name here’
git config --global ‘your email here’
git add .
git commit -m ‘some message’
create new repository in github and copy ssh link
git remote add origin ‘ssh link copied above’
git push -u origin master

That worked for me/


For me, the problem was that I had untracked files. Once you “git add …” + “git commit …”, you can rename the branch.


that was why! thanks!

1 Like

You are currently in detached head state. You must checkout a new branch to associate it with the current commit: git checkout -b new_branch

yea it worked for me

Thanks. I had the same issue. Did the first commit. pushed to remote origin first time fine. Thanks again for the tip

It helped me, I needed to make a commit first and after run git branch -M master