154

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.

Shang Wang
  • 24,909
  • 20
  • 73
  • 94

10 Answers10

289

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'
Lichi
  • 2,991
  • 2
  • 10
  • 5
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
knittl
  • 246,190
  • 53
  • 318
  • 364
  • 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 '13 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_. – frederickjh Mar 28 '18 at 08: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 '18 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. – frederickjh Mar 30 '18 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 – Md omer arafat Dec 14 '20 at 09:24
31

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

DimiDak
  • 4,820
  • 2
  • 26
  • 32
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.

yassine dotma
  • 697
  • 8
  • 10
9

First set your email and username config using:

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

Charan Shetty
  • 135
  • 1
  • 7
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.

Svend Hansen
  • 3,277
  • 3
  • 31
  • 50
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

rohit01
  • 101
  • 1
  • 3
1

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 .
SFARPak
  • 49
  • 4
-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! :)

Rasikh
  • 5
  • 1
  • 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. – tjheslin1 Mar 31 '22 at 06:25
  • This is a bad answer because it does not go into why it would work in the first place. – Saif Al Falah Oct 26 '22 at 11:49