终端中的 'git init -b <branch name>' 命令抛出 'unknown switch' 错误

'git init -b <branch name>' command in terminal is throwing an 'unknown switch' error

我正在尝试使用命令行将现有项目添加到 GitHub。我在终端的相关工作目录中,正在尝试使用 git init -b main 命令。

最初,我收到与 xcode:

有关的错误
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

我尝试了 xcode-select --install,但是更新服务器上的软件不可用,所以我从 https://developer.apple.com/download/more/ 下载了 'Command Line Tools for Xcode 12'。

现在进入 git init -b main 我得到以下信息:

error: unknown switch `b'
usage: git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]

--template <template-directory>
                      directory from which templates will be used
--bare                create a bare repository
--shared[=<permissions>]
                      specify that the git repository is to be shared amongst several users
-q, --quiet           be quiet
--separate-git-dir <gitdir>
                      separate git dir from working tree

我是运行git版本:2.24.3(苹果Git-128)

非常感谢任何帮助!

git 2.24 doesn't have option -b/--initial-branch. It was added in git 2.28。您需要升级才能使用该选项。

或者,如@matt 所说,创建一个 repo,然后重命名分支:

git init repo
cd repo
git branch -m master slave

发生这种情况是因为您的终端中名为 Xcode Select、运行 xcode-select --install 的工具无法使用 Git 无法解决这些问题正在工作。

在 git v2.28

之前
git init                              # ①
git symbolic-ref HEAD refs/heads/main # ②

git init之后,分支master其实并不存在。只有在至少有一次提交时才会创建分支。

② 这会将 .git/HEAD 更新为包含 ref: refs/heads/main 而不是 ref: refs/heads/master。或者,git checkout -b main.

git v2.28+

正如@phd 所说,-b/--initial-branch 选项是在 git v2.28 中添加的。 git 2.28 还引入了一个配置选项来指定您首选的默认分支:

git config --global init.defaultBranch main

GitHub's blog post 中了解有关新 init.defaultBranch 设置的更多信息。

-b标志仅在2.28或更高版本中可用,您需要升级您的Git。

在基于 debian 的 Linux 系统上,例如 Ubuntu,执行以下操作:

sudo add-apt-repository -y ppa:git-core/ppa
sudo apt update
sudo apt install git -y

如果您需要安装最新的 git 版本(在 Ubuntu 中)

sudo add-apt-repository -y ppa:git-core/ppa
sudo apt-get update
sudo apt-get install git -y

参考:https://gist.github.com/YuMS/6d7639480b17523f6f01490f285da509