如何使用“master”以外的默认分支名称创建 Git 存储库?

发布于 2025-01-14 04:46:40 字数 731 浏览 1 评论 0原文

Pro Git 书籍中,它说

“起源”并不特殊

就像分支名称“master”在 Git 中没有任何特殊含义一样,“origin”也没有任何特殊含义。 虽然“master”是运行 git init 时起始分支的默认名称,这是它被广泛使用的唯一原因,但“origin”是运行 git clone 时远程的默认名称。如果您改为运行 git clone -o booyah,那么您将把 booyah/master 作为默认远程分支。

这意味着,我们可以使用默认分支名称作为 mainmain-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

仅一夜美梦 2025-01-21 04:46:40

较新的 Git,新的存储库

自 git 版本 2.28.0 起,git init 命令现在采用 --initial-branch (或简称 -b) ) 范围。这两个命令创建一个新的 Git 存储库,其中包含一个名为“trunk”的分支,这对我来说总是比“master”更有意义(掌握什么?):

git init --initial-branch=trunk
git init -b trunk

这可以通过 init.defaultBranch 设置进行配置。如果我希望所有新的存储库都将“主干”作为默认分支:

git config --global init.defaultBranch trunk

旧的 Git,新的存储库

某些系统仍然安装了旧的 Git。我的 Debian 10 服务器(Buster,截至 2020 年 10 月的当前稳定版本)附带了 Git 2.20,它不支持 -b 选项。一种选择是创建存储库,然后更改分支名称。此技术适用于普通(非裸)存储库:

git init
git checkout -b trunk

这将创建一个新存储库,其中 trunk 作为当前分支而不是 master。分支 master 实际上并不存在——分支只有在至少一次提交后才会被创建。在分支创建之前,该分支仅存在于 .git/HEAD 中,这解释了为什么当您切换到 trunkmaster 分支会消失。

裸仓库

对于裸仓库,您无法运行 git checkout (这就是裸仓库的含义)。相反,您可以更改 HEAD 以指向不同的分支:

git init --bare
git symbolic-ref HEAD refs/heads/trunk

旧存储库

如果您已经提交,则可以运行 gitbranch -m 来代替:

git init
touch file.txt
git add file.txt
git commit -m 'commit 1'
git branch -m trunk

这会将分支从 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?):

git init --initial-branch=trunk
git init -b trunk

This is configurable with the init.defaultBranch setting. If I want all new repos to have "trunk" as the default branch:

git config --global init.defaultBranch trunk

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:

git init
git checkout -b trunk

This creates a new repository with trunk as the current branch instead of master. The branch master 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 the master branch will disappear when you switch to trunk.

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:

git init --bare
git symbolic-ref HEAD refs/heads/trunk

Old Repos

If you've already committed, you can run git branch -m instead:

git init
touch file.txt
git add file.txt
git commit -m 'commit 1'
git branch -m trunk

This renames the branch from master to trunk 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".

她说她爱他 2025-01-21 04:46:40

您可以间接配置 git init 以使用不同的默认分支:当前分支由 HEAD 定义,它“只是”一个文本文件,告诉 Git 哪个 ref 是当前的一个。

使用 init.templateDir,您可以要求 git init 使用不同的目录:

# ~/.config/git/config or ~/.gitconfig
[init]
    templateDir = ~/.config/git/template/

~/.config/git/template/HEAD 中,放置一行(+换行符):ref: refs/heads/main(默认为分支main)。

创建存储库时将 templateDir 的全部内容复制到 .git 目录中;默认值(此处为 /usr/share/git-core/templates)包含一些示例挂钩和其他文件,但例如,您可以使用新的模板目录来设置默认挂钩。

$ tree /usr/share/git-core/templates
/usr/share/git-core/templates
├── branches
├── description
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── fsmonitor-watchman.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   ├── pre-receive.sample
│   └── update.sample
└── info
    └── exclude

3 directories, 13 files

You can, indirectly, configure git init to use a different default branch: the current branch is defined by HEAD, which is “just” a textfile telling Git which ref is the current one.

Using init.templateDir, you can ask git init to use a different one:

# ~/.config/git/config or ~/.gitconfig
[init]
    templateDir = ~/.config/git/template/

and in ~/.config/git/template/HEAD, put a single line (+ linebreak): ref: refs/heads/main (to default to branch main).

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.

$ tree /usr/share/git-core/templates
/usr/share/git-core/templates
├── branches
├── description
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── fsmonitor-watchman.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   ├── pre-receive.sample
│   └── update.sample
└── info
    └── exclude

3 directories, 13 files
手心的温暖 2025-01-21 04:46:40

Git 2.28(2020 年 7 月 27 日发布)以来,引入了新的配置选项 init.defaultBranch 来取代硬编码术语 master

默认仍然是master

用户可以使用以下方法覆盖配置变量的默认值:

$ git config --global init.defaultBranch main

阅读 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 term master.

Default remains to master!

The user can override the default value of the configuration variable with:

$ git config --global init.defaultBranch main

Read the git doc chapter for further details Introducing init.defaultBranch

堇色安年 2025-01-21 04:46:40

如何使用“master”以外的默认分支名称创建 Git 存储库?

您将使用 Git 2.28(2020 年第 3 季度):现有存储库中主分支的名称以及新创建存储库中第一个分支使用的默认名称是可配置的,以便我们最终可以摆脱硬编码的“ 大师'。

以及 8 月的提醒2020 年来自 GitHub

2020 年 10 月 1 日,如果您尚未更改用户、组织或企业的新存储库的默认分支,它将自动从 master 更改为 主要
您可以随时选择退出此更改:

  • 对于用户,请访问https://github.com/settings/repositories页面
  • 对于组织所有者,请访问 https://github.com/organizations/YOUR-ORGANIZATION/settings/repository-defaults 页面
  • 对于企业管理员,请访问 https://github.com/enterprises/YOUR-ENTERPRISE/settings/member_privileges 页面

此更改是 GitHub 为支持想要重命名其默认分支的项目和维护者而做出的众多更改之一。
要详细了解我们所做的更改,请参阅 github/renaming

但回到 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 日)

init:允许指定初始分支名称新的存储库

签字人:Johannes Schindelin

越来越多的项目和公司希望更改其存储库的主分支名称(参见例如 Mislav Marohnić 的推文了解这方面的背景)。

要更改新存储库的分支名称,目前自动执行此操作的唯一方法是复制所有 Git 模板目录,然后将所需的默认分支名称硬编码到 .git/HEAD 文件,然后配置 init.templateDir 以指向那些复制的模板文件。

为了让这个过程变得不那么麻烦,我们引入一个新选项:--initial-branch=

git init --initial-branch=hello myLocalRepo
# or
git config --global init.defaultBranch hello
git init myLocalRepo

和:

init:允许设置初始值的默认值通过配置的分支名称

帮助者:Johannes Schindelin
帮助者:Derrick Stolee
签字人:Don Goodman-Wilson

我们刚刚引入了命令行选项--initial-branch=,以允许使用与硬编码分支不同的初始分支来初始化新存储库。

为了允许用户更永久地覆盖初始分支名称(即不必为每个 git init 调用手动指定名称),让我们引入 init.defaultBranch配置设置。


注意:commit 489947c,关于合并提交消息,已在 Git 2.29 中恢复,参见“如何自定义 git 的合并提交消息?”。
init.defaultBranch 设置保留。


这会影响子模块:

子模块:回退到远程的 HEAD 丢失远程..分支

帮助者:Philippe Blain
签字人:Johannes Schindelin

remote..branch未配置时, git submodule update 目前回退到使用分支名称 master
然而,更好的想法是使用远程 HEAD:在运行相当新的 Git 版本的所有 Git 服务器上,symref HEAD 指向主分支。

注意:t7419 表明可能存在预期的用例git submodule update --remote 将子模块更新到远程 master 分支,即使远程 HEAD 指向另一个分支。
可以说,这个补丁使行为更加直观,但这有可能导致模糊设置中的回归。

即便如此,应该可以修复此行为,而无需更长的过渡期:

  • git submodule update --remote 命令是并不常见。
  • 运行此命令时当前 Git 的行为完全令人困惑,除非远程存储库的当前分支 master(在这种情况下,建议的行为与旧的行为匹配)。< /里>
  • 如果用户由于行为更改而遇到回归问题,修复实际上很简单:将 submodule..branch 设置为 master 将恢复旧的行为。


请注意,在 Git 2.29(2020 年第 4 季度)中,contrib/ 中的测试已调整为最近对 fmt-merge-msg 的更改。

请参阅 commit b87528c(2020 年 8 月 3 日),作者:艾米丽·谢弗(nasamuffin
(由 Junio C Hamano -- gitster -- 合并于 提交 83b8250,2020 年 8 月 10 日)

恢复“contrib子树< /code>:调整测试以进行更改fmt-merge-msg"

签字人:Emily Shaffer

这将恢复提交 508fd8e8baf3e18ee40b2cf0b8899188a8506d07

6e6029a8fmt-merge-msg)中:允许再次省略合并目标)我们得到合并的行为默认情况下,“master”在合并消息末尾不包含“into 'master'”。不再需要此测试修复。

另外:

在 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 日)

测试:避免 的变化master 分支名称

签字人:Johannes Schindelin

术语“master”有着悠久的历史,它不断提醒人们种族不公正。 Git 项目不想让这种情况永久化,并且已经开始避免它。

测试套件对默认分支以外的分支使用此名称的变体。除了 t3200(我们刚刚在之前的提交中解决了这个问题)之外,这些实例可以以自动方式重命名,因为它们不需要在测试脚本之外进行任何更改,所以让我们这样做。

由于所涉及的分支与默认分支关系不大(如果有的话),我们选择使用完全独立的命名方案:topic_(它不能是 topic- 因为 t5515 使用带有该术语的 test_oid 机制,并且该机制在内部使用 shell 变量,其名称不能包含破折号)。

这个技巧是通过这个(GNU)sed 调用执行的:

$ sed -i 's/master\([a-z0-9]\)/topic_\1/g' t/t*.sh

并且,仍然使用Git 2.29:

请参阅 commit 538228e< /a>,提交 a15ad5d(2020 年 10 月 8 日),作者:约翰内斯·辛德林(dscho)
(由 Junio C Hamano -- gitster -- 合并于 提交62564ba,2020 年 10 月 8 日)

t1415:避免使用 main< /code> 作为参考名称

签字人:Johannes Schindelin

在准备将 init.defaultBranch 的后备更改为 main 的补丁系列时,我们不要使用 main 作为参考在此测试脚本中的名称。

否则,git -每个参考... | grep main(man ) 想要捕获这些引用也会意外地捕获 refs/heads/main

由于所讨论的引用是工作树本地引用(即每个工作树都有自己的工作树,就像 HEAD 一样),并且由于测试用例已经使用了名为“second<”的辅助工作树/code>”,让我们使用名称“first”来代替这些引用。

同时,调整谈论“存储库”的测试标题,而它们的意思是“工作树”。

How can I create a Git repository with the default branch name other than "master"?

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:

On October 1, 2020, if you haven't changed the default branch for new repositories for your user, organization, or enterprise, it will automatically change from master to main.
You can opt out of this change at any time:

  • For users, on the https://github.com/settings/repositories page
  • For organization owners, on the https://github.com/organizations/YOUR-ORGANIZATION/settings/repository-defaults page
  • For enterprise administrators, on the https://github.com/enterprises/YOUR-ENTERPRISE/settings/member_privileges page

This change is one of many changes GitHub is making to support projects and maintainers that want to rename their default branch.
To learn more about the changes we're making, see github/renaming.

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)

init: allow specifying the initial branch name for the new repository

Signed-off-by: Johannes Schindelin

There is a growing number of projects and companies desiring to change the main branch name of their repositories (see e.g. Mislav Marohnić's tweet for background on this).

To change that branch name for new repositories, currently the only way to do that automatically is by copying all of Git's template directory, then hard-coding the desired default branch name into the .git/HEAD file, and then configuring init.templateDir to point to those copied template files.

To make this process much less cumbersome, let's introduce a new option: --initial-branch=<branch-name>.

git init --initial-branch=hello myLocalRepo
# or
git config --global init.defaultBranch hello
git init myLocalRepo

And:

init: allow setting the default for the initial branch name via the config

Helped-by: Johannes Schindelin
Helped-by: Derrick Stolee
Signed-off-by: Don Goodman-Wilson

We just introduced the command-line option --initial-branch=<branch-name> to allow initializing a new repository with a different initial branch than the hard-coded one.

To allow users to override the initial branch name more permanently (i.e. without having to specify the name manually for each and every git init invocation), let's introduce the init.defaultBranch config setting.

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:

submodule: fall back to remote's HEAD for missing remote..branch

Helped-by: Philippe Blain
Signed-off-by: Johannes Schindelin

When remote.<name>.branch is not configured, git submodule update currently falls back to using the branch name master.
A much better idea, however, is to use the remote HEAD: on all Git servers running reasonably recent Git versions, the symref HEAD points to the main branch.

Note: t7419 demonstrates that there might be use cases out there that expect git submodule update --remote to update submodules to the remote master branch even if the remote HEAD points to another branch.
Arguably, this patch makes the behavior more intuitive, but there is a slight possibility that this might cause regressions in obscure setups.

Even so, it should be okay to fix this behavior without anything like a longer transition period:

  • The git submodule update --remote command is not really common.
  • Current Git's behavior when running this command is outright confusing, unless the remote repository's current branch is master (in which case the proposed behavior matches the old behavior).
  • If a user encounters a regression due to the changed behavior, the fix is actually trivial: setting submodule.<name>.branch to master will reinstate the old behavior.

Note that, with Git 2.29 (Q4 2020), tests in contrib/ are adjusted to the recent change to fmt-merge-msg.

See commit b87528c (03 Aug 2020) by Emily Shaffer (nasamuffin).
(Merged by Junio C Hamano -- gitster -- in commit 83b8250, 10 Aug 2020)

Revert "contrib: subtree: adjust test to change in fmt-merge-msg"

Signed-off-by: Emily Shaffer

This reverts commit 508fd8e8baf3e18ee40b2cf0b8899188a8506d07.

In 6e6029a8 (fmt-merge-msg: allow merge destination to be omitted again) we get back the behavior where merges against 'master', by default, do not include "into 'master'" at the end of the merge message. This test fix is no longer needed.

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)

tests: avoid variations of the master branch name

Signed-off-by: Johannes Schindelin

The term master has a loaded history that serves as a constant reminder of racial injustice. The Git project has no desire to perpetuate this and already started avoiding it.

The test suite uses variations of this name for branches other than the default one. Apart from t3200, where we just addressed this in the previous commit, those instances can be renamed in an automated manner because they do not require any changes outside of the test script, so let's do that.

Seeing as the touched branches have very little (if anything) to do with the default branch, we choose to use a completely separate naming scheme: topic_<number> (it cannot be topic-<number> because t5515 uses the test_oid machinery with the term, and that machinery uses shell variables internally, whose names cannot contain dashes).

This trick was performed by this (GNU) sed invocation:

$ sed -i 's/master\([a-z0-9]\)/topic_\1/g' t/t*.sh

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)

t1415: avoid using main as ref name

Signed-off-by: Johannes Schindelin

In preparation for a patch series that will change the fall-back for init.defaultBranch to main, let's not use main as ref name in this test script.

Otherwise, the git for-each-ref ... | grep main(man) which wants to catch those refs would also unexpectedly catch refs/heads/main.

Since the refs in question are worktree-local ones (i.e. each worktree has their own, just like HEAD), and since the test case already uses a secondary worktree called "second", let's use the name "first" for those refs instead.

While at it, adjust the test titles that talk about a "repo" when they meant a "worktree" instead.

惟欲睡 2025-01-21 04:46:40

如果您使用 Azure Devops:

  1. 在项目存储库下,选择“分支”。

  2. 在“分支”页面上,选择所需的新默认分支旁边的更多选项,然后选择设置为默认分支。

输入图片此处的描述

  1. 设置新的默认分支后,您可以根据需要删除之前的默认分支。

if you use Azure Devops:

  1. Under your project repo, select Branches.

  2. On the Branches page, select More options next to the new default branch you want, and choose Set as default branch.

enter image description here

  1. After you set the new default branch, you can delete the previous default if you want.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文