如何使用“master”以外的默认分支名称创建 Git 存储库?
在 Pro Git 书籍中,它说
“起源”并不特殊
就像分支名称“master”在 Git 中没有任何特殊含义一样,“origin”也没有任何特殊含义。 虽然“master”是运行 git init 时起始分支的默认名称,这是它被广泛使用的唯一原因,但“origin”是运行 git clone 时远程的默认名称。如果您改为运行 git clone -o booyah,那么您将把 booyah/master 作为默认远程分支。
这意味着,我们可以使用默认分支名称作为 main 或 main-branch 或类似的名称。我在 man git-init
中没有看到任何选项,该选项将使用不同的默认分支名称初始化我的 repo
。
GitHub 在其设置页面中展示了如何设置默认分支名称。但我并不是在谈论如何在任何特定的 Git 托管站点上设置它。我严格要求仅针对 Git,而不是针对任何特定的 Git 托管站点。
有办法做到这一点吗?
In the Pro Git book, it says
“origin” is not special
Just like the branch name “master” does not have any special meaning in Git, neither does “origin”. While “master” is the default name for a starting branch when you run git init which is the only reason it’s widely used, “origin” is the default name for a remote when you run git clone. If you run git clone -o booyah instead, then you will have booyah/master as your default remote branch.
That means, we can use our default branch name as main or main-branch or something like that. I didn't see any option in man git-init
which will initialize my repo
with a different default branch name.
GitHub shows how to set the default branch name in its settings page. But I am not talking about how to set it on any specific Git hosting site. I am strictly asking in terms of Git only, not in regards to any specific Git hosting site.
Is there a way to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
较新的 Git,新的存储库
自 git 版本 2.28.0 起,
git init
命令现在采用--initial-branch
(或简称-b
) ) 范围。这两个命令创建一个新的 Git 存储库,其中包含一个名为“trunk”的分支,这对我来说总是比“master”更有意义(掌握什么?):这可以通过 init.defaultBranch 设置进行配置。如果我希望所有新的存储库都将“主干”作为默认分支:
旧的 Git,新的存储库
某些系统仍然安装了旧的 Git。我的 Debian 10 服务器(Buster,截至 2020 年 10 月的当前稳定版本)附带了 Git 2.20,它不支持
-b
选项。一种选择是创建存储库,然后更改分支名称。此技术适用于普通(非裸)存储库:这将创建一个新存储库,其中
trunk
作为当前分支而不是master
。分支master
实际上并不存在——分支只有在至少一次提交后才会被创建。在分支创建之前,该分支仅存在于.git/HEAD
中,这解释了为什么当您切换到trunk
时master
分支会消失。裸仓库
对于裸仓库,您无法运行 git checkout (这就是裸仓库的含义)。相反,您可以更改 HEAD 以指向不同的分支:
旧存储库
如果您已经提交,则可以运行 gitbranch -m 来代替:
这会将分支从 master 重命名> 创建后到
trunk
。这看起来确实有点笨拙,因为根据存储库是否为空,机制会有所不同,但它确实有效。您也可以将其视为“创建新分支并删除
master
”。Newer Git, New Repo
Since git version 2.28.0 the
git init
command now takes a--initial-branch
(or-b
for short) parameter. These two commands create a new Git repo with a branch named "trunk", which always made more sense to me than "master" (master of what?):This is configurable with the
init.defaultBranch
setting. If I want all new repos to have "trunk" as the default branch:Older Git, New Repo
Some systems still have older Git installations. My Debian 10 server (Buster, the current stable version as of October 2020) comes with Git 2.20, which does not support the
-b
option. One option is to create the repository and then change the branch name. This technique works for normal (non-bare) repos:This creates a new repository with
trunk
as the current branch instead ofmaster
. The branchmaster
does not actually exist--the branches don't get created until they have at least one commit. Until the branch gets created, the branch only exists in.git/HEAD
, which explains why themaster
branch will disappear when you switch totrunk
.Bare Repos
For bare repos, you cannot run
git checkout
(that's what it means to be bare). Instead, you can change HEAD to point at a different branch:Old Repos
If you've already committed, you can run
git branch -m
instead:This renames the branch from
master
totrunk
once it's created.This does seem a bit clunky since the mechanism is different depending on whether the repository is empty, but it works. You can also approach it as "creating a new branch and deleting
master
".您可以间接配置
git init
以使用不同的默认分支:当前分支由HEAD
定义,它“只是”一个文本文件,告诉 Git 哪个 ref 是当前的一个。使用
init.templateDir
,您可以要求git init
使用不同的目录:在
~/.config/git/template/HEAD
中,放置一行(+换行符):ref: refs/heads/main
(默认为分支main
)。创建存储库时将
templateDir
的全部内容复制到.git
目录中;默认值(此处为/usr/share/git-core/templates
)包含一些示例挂钩和其他文件,但例如,您可以使用新的模板目录来设置默认挂钩。You can, indirectly, configure
git init
to use a different default branch: the current branch is defined byHEAD
, which is “just” a textfile telling Git which ref is the current one.Using
init.templateDir
, you can askgit init
to use a different one:and in
~/.config/git/template/HEAD
, put a single line (+ linebreak):ref: refs/heads/main
(to default to branchmain
).The whole contents of
templateDir
are copied to the.git
directory when creating a repository; the default (here/usr/share/git-core/templates
) contains some sample hooks and other files, but you can use your new template directory to setup default hooks, for example.自 Git 2.28(2020 年 7 月 27 日发布)以来,引入了新的配置选项
init.defaultBranch
来取代硬编码术语master
。默认仍然是
master
!用户可以使用以下方法覆盖配置变量的默认值:
阅读 git 文档章节以获取更多详细信息 介绍 init.defaultBranch
Since Git 2.28 (released July 27, 2020) a new configuration option,
init.defaultBranch
is being introduced to replace the hard-coded termmaster
.Default remains to
master
!The user can override the default value of the configuration variable with:
Read the git doc chapter for further details Introducing init.defaultBranch
您将使用 Git 2.28(2020 年第 3 季度):现有存储库中主分支的名称以及新创建存储库中第一个分支使用的默认名称是可配置的,以便我们最终可以摆脱硬编码的“
大师
'。以及 8 月的提醒2020 年来自 GitHub:
但回到 Git 本身:(2.28,2020 年第三季度)
请参阅 提交 508fd8e(2020 年 6 月 29 日),作者:Đoàn Trần Công Danh (
sgn
)。请参阅提交0068f21,提交a471214,提交 0cc1b47, 提交 32ba12d, 提交 6069ecc, 提交 f0a96e8, 提交 4d04658(2020 年 6 月 24 日),以及 提交 489947c(2020 年 6 月 23 日),作者:约翰内斯·辛德林(
dscho
)。请参阅 提交 8747ebb(2020 年 6 月 24 日),作者:唐古德曼-威尔逊 (
DEGoodmanWilson
)。(由 Junio C Hamano --
gitster
-- 合并于 提交11cbda2,2020 年 7 月 6 日)和:
注意:commit 489947c,关于合并提交消息,已在 Git 2.29 中恢复,参见“如何自定义 git 的合并提交消息?”。
init.defaultBranch
设置保留。这会影响子模块:
请注意,在 Git 2.29(2020 年第 4 季度)中,
contrib/
中的测试已调整为最近对fmt-merge-msg
的更改。请参阅 commit b87528c(2020 年 8 月 3 日),作者:艾米丽·谢弗(
nasamuffin
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 83b8250,2020 年 8 月 10 日)另外:
在 Git 2.29(2020 年第 4 季度)中,更新测试以删除单词“
master
”。请参阅提交f33f2d3,提交 b6211b8(2020 年 9 月 26 日),以及 提交 432f5e6, 提交 5a0c32b, 提交 659288c(2020 年 9 月 21 日),作者:约翰内斯·辛德林(
dscho
)。(由 Junio C Hamano --
gitster
-- 合并于 提交58138d3,2020 年 10 月 5 日)并且,仍然使用Git 2.29:
请参阅 commit 538228e< /a>,提交 a15ad5d(2020 年 10 月 8 日),作者:约翰内斯·辛德林(
dscho
)。(由 Junio C Hamano --
gitster
-- 合并于 提交62564ba,2020 年 10 月 8 日)You would use Git 2.28 (Q3 2020): the name of the primary branch in existing repositories, and the default name used for the first branch in newly created repositories, is made configurable, so that we can eventually wean ourselves off of the hardcoded '
master
'.And reminder from Aug. 2020 from GitHub:
But back to Git itself: (2.28, Q3 2020)
See commit 508fd8e (29 Jun 2020) by Đoàn Trần Công Danh (
sgn
).See commit 0068f21, commit a471214, commit 0cc1b47, commit 32ba12d, commit 6069ecc, commit f0a96e8, commit 4d04658 (24 Jun 2020), and commit 489947c (23 Jun 2020) by Johannes Schindelin (
dscho
).See commit 8747ebb (24 Jun 2020) by Don Goodman-Wilson (
DEGoodmanWilson
).(Merged by Junio C Hamano --
gitster
-- in commit 11cbda2, 06 Jul 2020)And:
Note: commit 489947c, about the merge commit message, has since been reverted in Git 2.29, see "how can I customize git's merge commit message?".
The
init.defaultBranch
setting remains.This impacts submodules:
Note that, with Git 2.29 (Q4 2020), tests in
contrib/
are adjusted to the recent change tofmt-merge-msg
.See commit b87528c (03 Aug 2020) by Emily Shaffer (
nasamuffin
).(Merged by Junio C Hamano --
gitster
-- in commit 83b8250, 10 Aug 2020)Also:
With Git 2.29 (Q4 2020), update the tests to drop word '
master
' from them.See commit f33f2d3, commit b6211b8 (26 Sep 2020), and commit 432f5e6, commit 5a0c32b, commit 659288c (21 Sep 2020) by Johannes Schindelin (
dscho
).(Merged by Junio C Hamano --
gitster
-- in commit 58138d3, 05 Oct 2020)And, still with Git 2.29:
See commit 538228e, commit a15ad5d (08 Oct 2020) by Johannes Schindelin (
dscho
).(Merged by Junio C Hamano --
gitster
-- in commit 62564ba, 08 Oct 2020)如果您使用 Azure Devops:
在项目存储库下,选择“分支”。
在“分支”页面上,选择所需的新默认分支旁边的更多选项,然后选择设置为默认分支。
if you use Azure Devops:
Under your project repo, select Branches.
On the Branches page, select More options next to the new default branch you want, and choose Set as default branch.