如何使用“master"以外的默认分支名称创建 Git 存储库? [英] How can I create a Git repository with the default branch name other than "master"?

查看:31
本文介绍了如何使用“master"以外的默认分支名称创建 Git 存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pro Git book 中,它说

起源"并不特殊

就像分支名称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.

这意味着,我们可以使用我们的默认分支名称 mainmain-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.

有没有办法做到这一点?

Is there a way to do that?

推荐答案

Newer Git, New Repo

从 git 版本 2.28.0 开始,git init 命令现在采用 --initial-branch(或简称 -b)参数.这两个命令创建了一个新的 Git 存储库,其分支名为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

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

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中,这就解释了为什么当你切换到trunkmaster分支会消失.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆