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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42871542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:40:50  来源:igfitidea点击:

How to create a git repository with the default branch name other than master?

gitversion-control

提问by Abhisek

In git-book, it says

git-book 中,它说

“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.

“起源”并不特殊

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

That means, we can use our default branch name as mainor main-branchor something like that. I didn't see any option in man git-initwhich will initialize my repowith a different default branch name.

这意味着,我们可以使用我们的默认分支名称作为mainmain-branch或类似的东西。我没有看到任何选项man git-init可以repo用不同的默认分支名称初始化我的。

Githubshows how to set the default branch name in her settings page. But I am not talking about how to set it in any specific git hosting site. I am strictly asking in terms of git only, not in regards to any specific git hosting site.

Github展示了如何在她的设置页面中设置默认分支名称。但我不是在谈论如何在任何特定的git 托管站点中设置它。我只是严格询问 git,而不是任何特定的git 托管站点

Is there a way to do that ?

有没有办法做到这一点 ?

回答by Dietrich Epp

As you noticed, there is no parameter for git initfor the branch name, so two commands will have to do.

正如您所注意到的,git init分支名称没有参数,因此必须执行两个命令。

git init
git checkout -b trunk

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

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

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

如果您已经提交,则可以运行git branch -m

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

This renames the branch from masterto trunkonce it's created.

这从重命名分支mastertrunk一旦创建它。

This does seem a bit clunky since the mechanism is different depending on whether the repository is empty, but it works.

这看起来确实有点笨拙,因为根据存储库是否为空,机制会有所不同,但它可以工作。

回答by Nicolas Braud-Santoni

You can, indirectly, configure git initto 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.

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

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

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

# ~/.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).

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

The whole contents of templateDirare copied to the .gitdirectory 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.

创建存储库时将 的全部内容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