简体   繁体   English

git分支-M主

[英]git branch -M main

One of the things github now recommends is changing the branch to main instead of master. github 现在建议的一件事是将分支更改为主分支而不是主分支。

The code given on the github site is: github 网站上给出的代码是:

git branch -M main

That never works for me, so I thought I would mention it here.这对我来说永远不起作用,所以我想我会在这里提到它。 I have a hard time believing this problem only occurs with me.我很难相信这个问题只发生在我身上。

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

You mention in your own answer that git branch -m main (or the same with -M ) only works once you have an initial commit.您在自己的回答中提到git branch -m main (或与-M相同)仅在您进行初始提交后才有效。

Alternatively, before creating any commits, use git checkout -b main to switch the name of the unborn branch to main .或者,在创建任何提交之前,使用git checkout -b main将未出生分支的名称切换为main

There is no functional difference between creating the initial commit, then renaming the branch, vs changing the unborn branch name, then making the initial commit.创建初始提交,然后重命名分支与更改未出生的分支名称,然后进行初始提交之间没有功能差异。 Commits do not remember which branch was the current branch when they were made, 1 so you're free to change branch names at any time.提交时不记得哪个分支是当前分支, 1因此您可以随时更改分支名称。 (Other people remember branch names, in their brains, and may have saved some branch names in clones, so it's best to do all these name changes before anyone else gets hold of these names. But that's outside your own Git.) (其他在他们的大脑中记得分支名称,并且可能在克隆中保存了一些分支名称,因此最好在其他人掌握这些名称之前完成所有这些名称更改。但这不在您自己的 Git 范围内。)


1 The git merge command does, however, generate a default merge message: 1但是, git merge命令会生成默认合并消息:

merge branch X [into Y]

and git pull generates a default merge message: git pull生成默认的合并消息:

merge branch X of 'url' [into Y]

where X is the argument you gave to git merge —with a URL added when using git pull to run git merge —and Y is present, and is the name of the current branch, if the current branch is not the designated "special" branch. where X is the argument you gave to git merge —with a URL added when using git pull to run git merge —and Y is present, and is the name of the current branch, if the current branch is not the designated "special" branch . This was hardcoded as master in the past, but is becoming configurable.这在过去被硬编码为master ,但正在变得可配置。 The end result of all of this is that you tend to get messages of the form merge branch feature when merging features into master / main , and messages of the form merge branch feature into develop when merging features into other branches.所有这一切的最终结果是,当您将功能合并到master / main时,您往往会收到格式merge branch feature merge branch feature into develop消息,而当您将功能合并到其他分支时,您往往会收到格式合并分支功能的消息。

Note that these autogenerated messages convey relatively little useful information , especially if you delete the feature branch after merging it.请注意,这些自动生成的消息传达的有用信息相对较少,尤其是在合并后删除feature分支时。 To take a particular example, suppose you reserve the name hotfix for a temporary branch on which hot-fixes are made.举一个特定的例子,假设您为一个临时分支保留名称hotfix ,在该分支上进行修补程序。 Then your repository will have the occasional "merge branch hotfix" commit, but each of these messages is for a different hotfix.然后,您的存储库将偶尔提交“合并分支修补程序”,但这些消息中的每一个都针对不同的修补程序。 The information being conveyed here is nearly useless—you need the date of the merge, not just the message, to find the right "hot bug".这里传达的信息几乎是无用的——你需要合并的日期,而不仅仅是消息,才能找到正确的“热门 bug”。 At worst, it may be worse than useless since it might send you looking at the wrong "hot bug".在最坏的情况下,它可能比无用更糟糕,因为它可能会让你看到错误的“热门错误”。 If you manually replace this with "merge fix for critical customer bug #1234", you get a useful message.如果您手动将其替换为“merge fix for critical customer bug #1234”,您会收到一条有用的消息。

(If your branch names include the bug-reference-number, then these messages are useful. The "into branch Y" part, which uses the current branch, still seems quite marginal to me, though.) (如果您的分支名称包含错误参考编号,那么这些消息很有用。不过,使用当前分支的“进入分支 Y”部分对我来说似乎仍然很边缘。)

There has to be at least one commit for this to work.必须至少有一个提交才能使其工作。

git status
On branch master

No commits yet


Do the first commit.做第一次提交。

git add *.html
git commit -m 'first'
[master (root-commit) 455481e] first
 1 file changed, 54 insertions(+)
 create mode 100644 start.html
git branch -m master main
git status
On branch main

Note a more complete explanation at How do I rename a local Git branch?请注意如何重命名本地 Git 分支?

It is important to point out that the only reason git creates an initial branch called master is due to the config setting init.defaultbranch set during installation of git-scm.需要指出的是,git 创建一个名为master的初始分支的唯一原因是由于在安装 git-scm 期间设置了配置设置init.defaultbranch If left to the default Let Git decide it will be master .如果保留默认让 Git 决定它将是master If you select the option Override the default... and you'd specified something else, for example the preset main , this will be the system wide default.如果您 select 选项Override the default...并且您指定了其他内容,例如预设的main ,这将是系统范围的默认值。 Select this and you can skip the step to name the initial branch during repo init. Select 这个,您可以跳过在 repo init 期间命名初始分支的步骤。

Check System setting检查系统设置

You can check the value of the system setting with:您可以使用以下命令检查系统设置的值:

git config --system init.defaultbranch

...returning the system wide set value. ...返回系统范围的设置值。

Override the system setting覆盖系统设置

You can override a setting on a global, user, level with:您可以使用以下命令覆盖全局用户级别的设置:

git config --global --add init.defaultbranch mistress

or, on a per project level with:或者,在每个项目级别:

git config --add init.defaultbranch mastress

Alternatively you could go and manually change the gitconfig file which—in windows—is stored with the programm in the etc folder.或者,您可以 go 并手动更改gitconfig文件,该文件在 windows 中与程序一起存储在etc文件夹中。 Locate the init.defaultbranch line and edit accordingly.找到 init.defaultbranch 行并进行相应的编辑。

GitHub uses "main" as the name of your primary branch . GitHub使用“main”作为主分支的名称。 It used to use "master", but git still defaults to "master".它曾经使用“master”,但git仍然默认为“master”。 You want to make sure that both git and GitHub are using the same branch name, so use "main".您要确保gitGitHub使用相同的分支名称,因此使用“main”。 The best way to accomplish this is to change your default branch name to "main":完成此操作的最佳方法是将默认分支名称更改为“main”:

$ git config --global init.defaultBranch main

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM