git:重命名本地分支失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18382986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:56:26  来源:igfitidea点击:

git: rename local branch failed

gitbranchrename

提问by Shang Wang

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 branchwithin the submodule, I have:

我不知道为什么我重命名本地分支的尝试失败了。我基本上克隆了项目,然后我在项目中也有一个子模块,我也下载了子模块代码。但是,当我git branch在子模块中使用时,我有:

* (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:

运行此命令后,git 给了我这个错误:

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

Anybody know why this happens? Thanks.

有人知道为什么会这样吗?谢谢。

回答by knittl

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

回答by Svend Hansen

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.

那会让它看起来像你期望的那样。