git: rename local branch failed

GitBranchRename

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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'

Solution 2 - Git

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

Solution 3 - Git

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

Solution 4 - Git

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.

Solution 5 - Git

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.

Solution 6 - Git

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.

Solution 7 - Git

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

Solution 8 - Git

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! :)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShang WangView Question on Stackoverflow
Solution 1 - GitLichiView Answer on Stackoverflow
Solution 2 - GitknittlView Answer on Stackoverflow
Solution 3 - GitDimiDakView Answer on Stackoverflow
Solution 4 - Gityassine dotmaView Answer on Stackoverflow
Solution 5 - GitCharan ShettyView Answer on Stackoverflow
Solution 6 - GitSvend HansenView Answer on Stackoverflow
Solution 7 - GitAntonio Francisco HernándezView Answer on Stackoverflow
Solution 8 - GitRasikhView Answer on Stackoverflow