2015-12-03 87 views
0

git checkout master游戏我下面的:error: pathspec 'master' did not match any file(s) known to git.混帐无法检出空主分支

下面是我开始回购创作步骤:

[email protected] ~/Desktop/repos/mine                
$ mkdir git-test                    

[email protected] ~/Desktop/repos/mine                
$ cd git-test                     

[email protected] ~/Desktop/repos/mine/git-test              
$ git init                      
Initialized empty Git repository in c:/Users/Daniel/Desktop/repos/mine/git-test/.git/   

[email protected] ~/Desktop/repos/mine/git-test (master)            
$ git checkout -b feat-a                  
Switched to a new branch 'feat-a'                

[email protected] ~/Desktop/repos/mine/git-test (feat-a)            
$ git checkout master                   
error: pathspec 'master' did not match any file(s) known to git.        

为什么出错?

回答

3

发生错误是因为没有分支名为master,因此git checkout试图将其重新解释为路径名,而且没有名为master的文件。

你可能想知道分支master去,因为git init创建它。 的答案git init并不真正创造它 - 至少,不是。什么git init与它的作用是一样的你的git checkout -b feat-a确实与feat-a:将它设置为“未出生的分支”,让你在不实际存在的一个分支。

这是创建的,否则,未出生的分支的第一个git commit当您首先创建了git commit时,您在feat-a,因此master(从未真正创建过),即使可能已停止。(哎呀,你没有做出第一次提交的是,我莫名其妙地变出一个凭空的。所以feat-a也未出生的,现在仍然是。让我们假设,寻找答案的其余部分,你没做git commit到创建第一个提交。)

您仍然可以创建指向当前提交的git checkout -b master,或者git checkout --orphan master,它将“创建”为未出生,等待第一次提交。这两者之间的区别在于,如果没有--orphan,由于现在存在一个提交,git可以并且将在当前提交时完全创建分支。使用--orphan,或者在特殊的情况下,git不会或不能创建指向现有提交的新分支名称。

0

虽然Git的默认分支被命名为master,直到有一个提交就可以了,没有什么的名称为指向,因此换你HEADgit checkout -b不同的分支失去master别名HEAD曾经有过。

您可以在一个新的仓库看到这一点show-ref

git init . 
git show-ref 
# (no output) 

如果检查出一个新的分支名称和提交的东西,你会看到它得到root-commit名称,然后一提的是可用的。

git checkout -b branch 
git commit --allow-empty -m 'First commit' 
[branch (root-commit) 0085654] First commit 
git show-ref 
0085654104e3cfcbbd836555c94ef9ca8e4c26fa refs/heads/branch 

通常情况下,这种情况发生在master,因为你不刻意去先改变分支,但现在根本提交你的资料库是branch,你会从那里。如果您退出master,它将从branch克隆,就像任何其他分支一样。

$ git checkout -b master 
Switched to a new branch 'master' 
$ g show-ref 
0085654104e3cfcbbd836555c94ef9ca8e4c26fa refs/heads/branch 
0085654104e3cfcbbd836555c94ef9ca8e4c26fa refs/heads/master