156

I don't know why my attempt of renaming local branch failed. I basically cloned the project, then I also have a submodule within the project, and I downloaded the submodule code as well. However, when I use git branch within the submodule, I have:

* (no branch)
  master

The code looks like I'm on another branch but the output shows that it doesn't have a name. Then I searched online to find how to rename local branch and I got this:

git branch -m <newname>

After I run this command git gave me this error:

error: refname refs/heads/HEAD not found
fatal: Branch rename failed

Anybody know why this happens? Thanks.

10 Answers 10

291

I get into this issue too. The reason is that I didn't have any commit on this git repository.

When I run the command git branch -M main. I get the following error message.

error: refname refs/heads/master not found
fatal: Branch rename failed

After I add my first commit by the following command, all things work.

git add .
git commit -m 'Init'
7
  • 1
    This was the explanation I was looking for. The context is vague so I'm not surprised there are other answers, but good job on this and thank you!! Oct 10, 2020 at 0:36
  • 29
    Github should consider adding that part to their guide
    – SuperCode
    Nov 10, 2020 at 6:53
  • 1
    Yes, this was the culprit!
    – ruslaniv
    Jan 3, 2021 at 8:12
  • an alternative is to manually edit .git/HEAD to read ref: refs/heads/main, rather than refs/heads/master.
    – jyn
    Apr 4, 2022 at 21:23
  • This works, missed it from my end. Good one
    – Sanjay
    May 30, 2022 at 5:00
125

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
5
  • You are right! I thought git doesn't recognize branches with the same name as one branch, and your command is creating a new branch, but turns out it works. Thanks.
    – Shang Wang
    Aug 22, 2013 at 14:29
  • This solution also worked for me in a situation that I thought was not related. I had created a new git repo and without any commits tried to rename the master branch and got the same error message. Using git checkout -b newbranch effectively renamed the master branch. I am guessing this means that before you make the first commit in a new repository you are in a detached head state. Mar 28, 2018 at 8:51
  • 1
    @frederickjh: no, when creating a new repository you are on master branch by default. It might be possible though that it is not possible to rename the master branch, if there are no commits yet.
    – knittl
    Mar 28, 2018 at 19:13
  • @knittl, true that when you create a new repository you are on the master branch. That was exactly my situation. When no commits have been made then there is no reference pointing to refs/heads/HEAD which is why the error message is displayed when one tries to rename the branch. So is it a detached head state? Seeing as there is no pointer to HEAD it could be, however I will leave that to the git experts here to hash out! Pun intended. Mar 30, 2018 at 14:17
  • If you just created a repo and wanna pus your code, go with the basic commands -> git init -> git add remote .. -> git commit -m "first commit" -> git push -u origin master Dec 14, 2020 at 9:24
32

I thought it was a conflict of "git init" creating master branch and github's (new) "main".

After:

git add .
git commit -m "first commit" 

I was able to git branch -M main

enter image description here

13

You can change the name from master to main in few steps, locally before you even make a commit.

  1. Navigate to the directory where your project sits.
  2. In it, show hidden file since by default, .git would be hidden.
  3. Inside .git, there is a file, HEAD, open it in a text editor. You'd see, ref: refs/heads/master.
  4. Simple enough, change, master to main.

We just renamed the master branch as main. Verify this merely by entering, git branch from the terminal.

1
  • This is exactly what I was looking for, thanks! Mar 13, 2021 at 13:47
9

First set your email and username config using:

git config --global user.email “[email protected]”
git config --global user.name “Your Name”

Then add your files:

git add .

Then make your first commit :

git commit -m "Initial commit"

And now run the command :

git branch -M main

It worked for me this way.

7

My guess is that you're not on a branch named "(no branch)", but rather not on a branch.

If you first checkout master:

git checkout master

and then create a new branch:

git checkout -b new_branch

that would make it look like you'd expect.

2

I also got that error but I fixed it with: git commit -m"your commit" before : git branch -M main and it worked correctly

2

I also facing the same issue.

you can run following command to switch from master to main.

git add .
git commit -m "Init"
git branch -m main

running the above code

Finally switch from master to main

2

You need to have some changes as well before you can commit and add files. Otherwise you will still receive this message.

If you have no changes make some or create blank file if you have nothing in it.

touch blank.txt

and than

git commit -m "Project Init"
git add .
-2

Try this:

  1. git config --global user.email “your-email”

  2. git config --global user.name “your-username”

  3. git commit -m "TypeScript React using Tailwind"

  4. git branch -M main

  5. git push -u origin main

it must work! :)

2
  • This is a duplicate of an of existing answer by @Charan Shetty. When answering older questions that already have answers, please make sure you provide either a novel solution or a significantly better explanation than existing answers. Remember to review all existing answers first.
    – Algamest
    Mar 31, 2022 at 6:25
  • This is a bad answer because it does not go into why it would work in the first place. Oct 26, 2022 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.