在 git 中得到了“致命的:分支‘master’不存在”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46915350/
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
got 'fatal: branch 'master' does not exist' in git
提问by Zander Wong
using git
for a while, it's very powerful and beautiful..
用git
了一段时间,很强大很漂亮。。
but also got some confused about it:
但也有一些困惑:
it should under branch master after I init a git repo, isn't it?
在我初始化一个 git repo 后,它应该在分支 master 下,不是吗?
but git branch -a
, i got nothing man.
and I got fatal: branch 'master' does not exist
when I try to set upstream for my branch.
但是git branch -a
,我什么都没有,伙计。当我尝试为我的分支设置上游时
,我得到了fatal: branch 'master' does not exist
。
users@debian MINGW64 ~/Desktop/taste
$ git init
Initialized empty Git repository in C:/Users/users/Desktop/taste/.git/
users@debian MINGW64 ~/Desktop/taste (master)
$ git remote add origin [email protected]:greedev/Test.git
users@debian MINGW64 ~/Desktop/taste (master)
$ git branch -u origin/master
fatal: branch 'master' does not exist
users@debian MINGW64 ~/Desktop/taste (master)
$ git branch -a
users@debian MINGW64 ~/Desktop/taste (master)
$ git fetch
The authenticity of host 'gitee.com (120.55.226.24)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com,120.55.226.24' (ECDSA) to the list of know n hosts.
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 7 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (7/7), done.
From gitee.com:greedev/Test
* [new branch] master -> origin/master
users@debian MINGW64 ~/Desktop/taste (master)
$ git branch -a
remotes/origin/master
users@debian MINGW64 ~/Desktop/taste (master)
$ git branch -u origin/master
fatal: branch 'master' does not exist
回答by torek
TL;DR
TL; 博士
You can git checkout master
at this point.
此时你可以git checkout master
。
Longer description (but still not that long)
更长的描述(但仍然没有那么长)
You are doing this the hard way.
你这样做很困难。
In the future, instead of:
在未来,而不是:
mkdir repo
cd repo
git init
git remote add origin <url>
git fetch origin
git checkout master
you can simply run:
你可以简单地运行:
git clone <url> repo
since the six commands above are pretty much what git clone
does.
因为上面的六个命令几乎是什么git clone
。
After the first three steps—creating a new, totally-empty repository—you have a repository that is in a peculiar state: it has no commits, so it has no branches. At the same time, it doeshave a current branch, which is master
.
在前三个步骤(创建一个新的、完全空的存储库)之后,您将拥有一个处于特殊状态的存储库:它没有提交,因此没有分支。同时,它确实有一个当前分支,即master
。
In other words, the current branch is a branch that does not exist.
换句话说,当前分支是一个不存在的分支。
This state is unusual, but normal. If you run git checkout --orphan newbranch
, you put your Git repository into that same state:1on a branch that does not exist. The branch gets created once there is a commit hash to store under the branch name.
这种状态很不寻常,但很正常。如果您运行git checkout --orphan newbranch
,您将您的 Git 存储库置于相同的状态:1在不存在的分支上。一旦有一个提交哈希存储在分支名称下,分支就会被创建。
Whenever you run git checkout <name>
and there is no branch named <name>
, Git checks to see if there is exactly oneremote-tracking branch such as origin/<name>
. If so, Git creates a newbranch named <name>
that points to the same commit as origin/<name>
and that has origin/<name>
as its upstream.
每当您运行git checkout <name>
并且没有名为 的分支时<name>
,Git 就会检查是否只有一个远程跟踪分支,例如origin/<name>
. 如果是这样,Git会创建一个新的命名的分支<name>
,它指向同一个commit的origin/<name>
,并且具有origin/<name>
作为其上游。
Since this last step—git checkout master
when master
does not actually exist yet—is the final step of git clone
, git clone
will also create a new branch master
that tracks the remote-tracking branch origin/master
.
由于这最后一步(git checkout master
当master
实际上还不存在时)是 的最后一步git clone
,git clone
因此还将创建一个master
跟踪远程跟踪分支的新分支origin/master
。
1Note that you retain the current index / staging-area content. This is true for the new empty repository as well, but since it's a new empty repository, the index / staging-area is also empty, and "retaining the empty set" does not feel much like retainment.
1请注意,您保留了当前索引/暂存区的内容。对于新的空存储库也是如此,但由于它是一个新的空存储库,因此索引/暂存区也是空的,“保留空集”感觉不像保留。
回答by ElpieKay
After you run git init
, the master
you see is not completely created. It doesn't exist as it hasn't pointed to any commit yet. I once read that it was designed. But I think it's a puzzling bug. If you run git branch
, it returns nothing.
运行后git init
,master
您看到的未完全创建。它不存在,因为它还没有指向任何提交。我曾经读到它是设计的。但我认为这是一个令人费解的错误。如果你运行git branch
,它什么都不返回。
After you run git fetch
, a following git checkout master
does the job. It is equivalent to:
运行后git fetch
,以下git checkout master
工作即可完成。它相当于:
git branch master origin/master
git checkout master
git branch -u origin/master
回答by Pbd
You are probably looking for git checkout master
and also some tutorials.
您可能正在寻找git checkout master
以及一些教程。