git:重命名本地分支失败

xurqigkl  于 2022-12-10  发布在  Git
关注(0)|答案(9)|浏览(342)

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

* (no branch)
  master

这段代码看起来像是在另一个分支上,但输出显示它没有名称。然后我在网上搜索如何重命名本地分支,得到了以下结果:

git branch -m <newname>

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

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

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

2uluyalo

2uluyalo1#

我也遇到了这个问题,原因是我在这个git仓库上没有提交任何东西。
当我运行命令git branch -M main时,我得到以下错误消息。

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

在我用下面的命令添加了第一次提交之后,所有的事情都工作了。

git add .
git commit -m 'Init'
axzmvihb

axzmvihb2#

您当前处于 detached head state。您必须 checkout 一个新的分支才能将其与当前提交相关联:

git checkout -b new_branch
tzxcd3kk

tzxcd3kk3#

我认为这是“git init”创建master分支和github(new)的“main”的冲突。
之后:

git add .
git commit -m "first commit"

我可以git branch -M main

8yparm6h

8yparm6h4#

只需几个步骤,您就可以在提交之前,在本地将名称从master更改为main
1.导航到项目所在的目录。
1.在其中,显示隐藏文件,因为默认情况下,.git将被隐藏。
1.在.git中,有一个文件HEAD,用文本编辑器打开它,你会看到ref: refs/heads/master
1.很简单,将master更改为main
我们刚刚将master分支重命名为main。只需在终端输入git branch即可验证这一点。

v1uwarro

v1uwarro5#

首先使用以下命令设置电子邮件和用户名配置:

git config --global user.email “you@example.com”
git config --global user.name “Your Name”

然后添加文件:

git add .

然后进行第一次提交:

git commit -m "Initial commit"

现在运行命令:

git branch -M main

这对我很有效。

nle07wnf

nle07wnf6#

我的猜测是,你不是在名为“(no branch)"的分支上,而是不在一个分支上。
如果您首先 checkout 主文件:

git checkout master

然后创建一个新分支:

git checkout -b new_branch

让它看起来像你想的那样。

kulphzqa

kulphzqa7#

我也遇到了这个错误,但我用以下代码修复了它:git commit -m"your commit"之前:git branch -M main,并且工作正常

0x6upsns

0x6upsns8#

您可以运行以下命令从主服务器切换到主服务器。

git add .
git commit -m "Init"
git branch -m main

bwitn5fc

bwitn5fc9#

试试这个:

  1. git config --全球user.email“您的电子邮件”
  2. git config --全局user.name“您的用户名”
  3. git commit -m“使用顺风进行TypeScriptReact”
  4. git分支-M主目录
  5. git push -u原始main
    它必须工作!:)

相关问题