为什么我不能在第一次提交之前重命名 master?

问题描述 投票:0回答:1

在尝试重命名我的主分支(git分支-M main)时,我偶然发现了以下错误:“

error: refname refs/heads/master not found

第一次提交后,我可以毫无问题地重命名我的分支。此时,我想知道为什么我无法重命名 master 分支。

错误消息表明没有引用“master”。这是合乎逻辑的,因为它没有指向的提交。然而,值得注意的是,分支“master”本身存在,正如我可以运行“

git branch --show-current
”这一事实所证明的那样。

那么发生了什么?为什么 Git 在重命名分支时需要引用?有没有办法在第一次提交之前重命名 master?(此外: git:重命名本地分支失败


另外:

以下评论表明 Git 处于“分离头状态”,但我发现解释不清楚。 git:重命名本地分支失败

我认为分离头是“HEAD 没有指向分支(但可能指向提交)”。 但就我而言,不仅 HEAD 而且 master 都没有指向任何地方

git git-branch
1个回答
0
投票

为什么我不能在第一次提交之前重命名 master?

因为您使用的 Git 版本早于 2.30.0。从那时起,您应该能够在进行任何提交之前重命名初始分支。例如:

/
$ git --version
git version 2.30.0.windows.1

/
$ git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /

/ (master)
$ git branch -m main

15:24:18 / (main)
$
© www.soinside.com 2019 - 2024. All rights reserved.