如何创建具有默认分支名称(而不是"master")的Git存储库? [英] How can I create a Git repository with the default branch name other than "master"?
问题描述
在 Pro Git书中,它说
原产地"并不特殊
"origin" is not special
就像分支名称"master"在Git中没有任何特殊含义一样,"origin"也没有. 虽然"master"是运行git init时启动分支的默认名称,这是它被广泛使用的唯一原因,但"origin"是运行git clone时远程的默认名称.如果改为运行git clone -o booyah,则将booyah/master作为默认远程分支.
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.
这意味着,我们可以将默认分支名称用作 main 或 main-branch 或类似名称.我没有在man git-init
中看到任何选项,它将使用不同的默认分支名称初始化我的repo
.
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展示了如何在其默认设置分支名称设置页面.但是我不是在谈论如何在任何特定的 Git托管站点上进行设置.我只问Git方面的问题,而不是针对任何特定的 Git托管站点.
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.
有办法吗?
推荐答案
New Git,新仓库
从git版本2.28.0开始,git init
命令现在使用一个--initial-branch
(或简称为-b
)参数.这两个命令使用名为"trunk"的分支来创建新的Git存储库,这对我而言总是比"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
这可以通过init.defaultBranch
设置进行配置.如果我希望所有新的回购都带有"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
旧Git,新仓库
某些系统仍具有较旧的Git安装.我的Debian 10服务器(Buster,截至2020年10月为 current 稳定版本)随附Git 2.20,它不支持-b
选项.一种选择是创建存储库,然后更改分支名称.此技术适用于普通(非裸机)存储库:
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
这将使用trunk
作为当前分支而不是master
创建新的存储库.分支master
实际上并不存在-分支只有在至少提交一次之后才会创建.在创建分支之前,该分支仅存在于.git/HEAD
中,这说明了为什么切换到trunk
时master
分支会消失的原因.
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
.
对于裸仓库,您不能运行git checkout
(这就是裸仓库的意思).相反,您可以将HEAD更改为指向其他分支:
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
旧存储库
如果已经提交,则可以运行git branch -m
:
git init
touch file.txt
git add file.txt
git commit -m 'commit 1'
git branch -m trunk
创建分支后,它将分支从master
重命名为trunk
.
This renames the branch from master
to trunk
once it's created.
这似乎有点笨拙,因为该机制根据存储库是否为空而有所不同,但它可以工作.您也可以将其称为创建新分支并删除master
".
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
".
这篇关于如何创建具有默认分支名称(而不是"master")的Git存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!