为什么我的“git 分支”没有主人?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3623755/
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-10 09:01:11  来源:igfitidea点击:

Why does my 'git branch' have no master?

git

提问by aberrant80

I'm a git newbie and I keep reading about a "master" branch. Is "master" just a conventional name that people used or does it have special meaning like HEAD?

我是 git 新手,我一直在阅读有关“主”分支的信息。“大师”只是人们使用的常规名称还是具有特殊含义HEAD

When I do git branchon the clone that I have, I only see 1 single branch - the one I'm on. No "master" at all. If I type git checkout master(as I see in alot of tutorials or guides), I get

当我git branch对我拥有的克隆进行操作时,我只看到 1 个分支 - 我所在的分支。根本没有“主人”。如果我输入git checkout master(正如我在很多教程或指南中看到的那样),我会得到

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

I'm just confused as to why my clone doesn't have a masterthat everyone seems to imply that it always exists.

我只是很困惑为什么我的克隆没有master每个人似乎都暗示它总是存在的。

采纳答案by Amber

Most Git repositories use masteras the main (and default) branch - if you initialize a new Git repo via git init, it will have masterchecked out by default.

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

However, if you clone a repository, the default branch you have is whatever the remote's HEADpoints to (HEADis actually a symbolic refthat points to a branch name). So if the repository you cloned had a HEADpointed to, say, foo, then your clone will just have a foobranch.

但是,如果您克隆存储库,则您拥有的默认分支是远程HEAD指向的任何分支(HEAD实际上是指向分支名称的符号引用)。因此,如果您克隆的存储库有一个HEAD指向,例如foo,,那么您的克隆将只有一个foo分支。

The remote you cloned from might still have a masterbranch (you could check with git ls-remote origin master), but you wouldn't have created a local version of that branch by default, because git cloneonly checks out the remote's HEAD.

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

回答by Bunyk

To checkout a branch which does not exist locally but is in the remote repo you could use this command:

要签出本地不存在但在远程仓库中的分支,您可以使用以下命令:

git checkout -t -b master origin/master

回答by Matt Curtis

masteris just the name of a branch, there's nothing magic about it except it's created by default when a new repository is created.

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

You can add it back with git checkout -b master.

您可以使用git checkout -b master.

回答by eacousineau

I actually had the same problem with a completely new repository. I had even tried creating one with git checkout -b master, but it would not create the branch. I then realized if I made some changes and committed them, git created my master branch.

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

回答by antoniob

In my case there was a developbranch but no masterbranch. Therefore I cloned the repository pointing the newly created HEAD to the existing branch. Then I created the missing masterbranch and update HEAD to point to the new master branch.

就我而言,有一个开发分支,但没有分支。因此,我克隆了将新创建的 HEAD 指向现有分支的存储库。然后我创建了缺少的master分支并更新 HEAD 以指向新的 master 分支。

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

回答by digit

if it is a new repo you've cloned, it may still be empty, in which case:

如果它是您克隆的新存储库,则它可能仍然是空的,在这种情况下:

git push -u origin master

git push -u origin master

should likely sort it out.

应该可以解决它。

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

(在我的情况下。不确定这是同一个问题,我想我应该发布这个以防万一。可能会帮助其他人。)

回答by Nick ONeill

I ran into the same issue and figured out the problem. When you initialize a repository there aren't actually any branches. When you start a project run git add .and then git commitand the master branch will be created.

我遇到了同样的问题并找出了问题所在。当您初始化存储库时,实际上没有任何分支。当你开始一个项目运行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.

没有检查任何东西,你就没有主分支。在这种情况下,您需要按照其他人建议的步骤进行操作。

回答by Zelphir Kaltstahl

It seems there must be at least one local commit on the master branch to do:

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

git push -u origin master

So if you did git init .and then git remote add origin ..., you still need to do:

所以如果你做了git init .然后git remote add origin ...,你仍然需要做:

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