为什么我的“ Git 树枝”没有主人?

我是个饭桶新手,我一直在读关于“大师”分支的报道。“主人”只是一个传统的名称,人们使用或它有特殊的意义,如 HEAD

当我在我的克隆上做 git branch的时候,我只看到一个单独的分支——我所在的那个。根本没有“主人”这个称呼。如果我键入 git checkout master(正如我在许多教程或指南中看到的) ,我得到

error: pathspec 'master' did not match any file(s) known to git.

我只是不明白为什么我的克隆体没有一个 master,似乎每个人都暗示它总是存在。

223545 次浏览

大多数 Git 存储库使用 master作为主(和默认)分支-如果您通过 git init初始化一个新的 Git 回购,它将在默认情况下签出 master

但是,如果您克隆了一个存储库,那么您拥有的默认分支是远程 HEAD指向的任何分支(HEAD实际上是指向分支名称的 象征性的裁判)。因此,如果所克隆的存储库指向一个 HEAD,比如说 foo,那么所克隆的存储库将只有一个 foo分支。

从中复制的远程可能仍然有一个 master分支(可以用 git ls-remote origin master检查) ,但是默认情况下不会创建该分支的本地版本,因为 git clone只检查远程的 HEAD

master只是一个分支的名称,它没有什么神奇的地方,除了在创建新存储库时默认创建它。

你可以用 git checkout -b master把它加回去。

实际上,我在一个全新的存储库中遇到了同样的问题。我甚至尝试用 git checkout -b master创建一个分支,但是它不能创建分支。然后我意识到,如果我做了一些更改并提交它们,git 就会创建我的主分支。

如果你克隆的是一个新的回购协议,它可能仍然是空的,在这种情况下:

git push -u origin master

应该能解决。

(did in my case. not sure this is the same issue, thought i should post this just incase. might help others.)

要检出一个本地不存在但是在远程回购中的分支,您可以使用以下命令:

git checkout -t -b master origin/master

在我的案例中,有一个 开发分支,但没有 师父分支。因此,我克隆了将新创建的 HEAD 指向现有分支的存储库。然后,我创建了缺失的 师父分支,并更新了 HEAD 以指向新的主分支。

git clone git:repositoryname --branch otherbranch
git checkout -b master
git update-ref HEAD master
git push --set-upstream origin master

我遇到了同样的问题,并找出了问题所在。在初始化存储库时,实际上没有任何分支。当您开始一个项目运行 git add .,然后 git commit和主分支将被创建。

Without checking anything in you have no master branch. In that case you need to follow the steps other people here have suggested.

似乎主分支上必须至少有一个本地提交:

git push -u origin master

因此,如果你先做 git init .然后做 git remote add origin ...,你仍然需要做:

git add ...
git commit -m "..."

If you create a new repository from the Github web GUI, you sometimes get the name 'main' instead of 'master'. By using the command git status from your terminal you'd see which location you are. In some cases, you'd see origin/main.

如果你想通过 CLI 把你的应用程序推向云服务,那么使用“ main”,而不是“ master”。

例如: git push heroku main

我也有同样的问题。我执行了“ git init”,但是没有创建 main/master 分支。可能是因为我使用了一个具有其他名称的分支,而这个分支成为了默认分支。 对我有效的解决办法是:

  1. 我创建了“ main”分支

git checkout -b main

  1. I went to the option Settings->Branches of my directory and changed it to the new branch "main" enter image description here

  2. With this change, Github itself notified me that my default branch (main) was behind my other branch and opened the window for me to confirm a PR applying the changes to the branch main. And following this, merging the pull request. enter image description here