git: rename local branch failed
Asked Answered
V

10

157

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.

Vogel answered 22/8, 2013 at 14:14 Comment(0)
S
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
Sigvard answered 22/8, 2013 at 14:17 Comment(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.Vogel
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.Theurich
@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.Sigvard
@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.Theurich
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 masterGrantor
T
296

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'
Tantalizing answered 5/10, 2020 at 8:55 Comment(7)
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!!Corrinecorrinne
Github should consider adding that part to their guideSkite
Yes, this was the culprit!Purdum
an alternative is to manually edit .git/HEAD to read ref: refs/heads/main, rather than refs/heads/master.Antiproton
This works, missed it from my end. Good oneNitid
Once you do your initial commit you will be able to change the branch name, so happy to have found this thread. ThanksStop
For those who think about renaming before even having files. git commit -m "Init commit" --allow-emptyImpalpable
S
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
Sigvard answered 22/8, 2013 at 14:17 Comment(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.Vogel
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.Theurich
@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.Sigvard
@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.Theurich
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 masterGrantor
J
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

Joniejonina answered 5/10, 2020 at 19:29 Comment(0)
A
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.

Adala answered 10/11, 2020 at 12:38 Comment(1)
This is exactly what I was looking for, thanks!Charmion
G
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.

Gram answered 19/7, 2021 at 9:48 Comment(0)
I
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.

Intestine answered 22/8, 2013 at 14:17 Comment(0)
K
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

Kettle answered 10/6, 2021 at 16:50 Comment(0)
M
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

Mueller answered 3/12, 2022 at 13:7 Comment(0)
V
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 .
Votyak answered 21/1, 2023 at 9:56 Comment(0)
E
-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! :)

Ekaterinoslav answered 29/3, 2022 at 8:11 Comment(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.Peppy
This is a bad answer because it does not go into why it would work in the first place.Blinny

© 2022 - 2024 — McMap. All rights reserved.