Error: refname refs/heads/master not found

Hi

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.

2 Likes

Can I post the link to the solution here? Its an external source… and its in Portuguese:
https://pt.stackoverflow.com/questions/388005/problema-na-hora-de-fazer-push-no-github

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?

14 Likes

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

1 Like

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).

5 Likes

Run

git config --global user.email “you@example.com
git config --global user.name “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.

6 Likes

Yeah, this here is the simpler solution.

git init
git config --global user.name ‘your name here’
git config --global user.email ‘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/

3 Likes

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

1 Like

that was why! thanks!