Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: rename local branch failed

Tags:

git

branch

rename

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.

like image 956
Shang Wang Avatar asked Aug 22 '13 14:08

Shang Wang


People also ask

Can we rename the branch name in git?

Git Branch Rename Command The steps to change a git branch name are: Rename the Git branch locally with the git branch -m new-branch-name command. Push the new branch to your GitHub or GitLab repo. Delete the branch with the old name from your remote repo.

How do I rename a remote branch?

There isn't a way to directly rename a Git branch in a remote repository. You will need to delete the old branch name, then push a branch with the correct name to the remote repository. The output confirms that the branch was deleted.

What happens when you rename a branch?

About renaming branches When you rename a branch on GitHub.com, any URLs that contain the old branch name are automatically redirected to the equivalent URL for the renamed branch.


1 Answers

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' 
like image 151
lichi.chen Avatar answered Nov 04 '22 11:11

lichi.chen