git无法签出空主分支

时间:2015-12-03 04:19:28

标签: git

git checkout master向我展示以下内容:error: pathspec 'master' did not match any file(s) known to git.

以下是我从回购创建开始的步骤:

Daniel@DOVE ~/Desktop/repos/mine                                                              
$ mkdir git-test                                                                              

Daniel@DOVE ~/Desktop/repos/mine                                                              
$ cd git-test                                                                                 

Daniel@DOVE ~/Desktop/repos/mine/git-test                                                     
$ git init                                                                                    
Initialized empty Git repository in c:/Users/Daniel/Desktop/repos/mine/git-test/.git/         

Daniel@DOVE ~/Desktop/repos/mine/git-test (master)                                            
$ git checkout -b feat-a                                                                      
Switched to a new branch 'feat-a'                                                             

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

为什么会出错?

2 个答案:

答案 0 :(得分:4)

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

你可能想知道分支master去了哪里,因为git init创建它。 的答案是git init并没有真正创造它 - 至少,不完全。 git init对它的作用与git checkout -b feat-afeat-a的作用相同:将其设置为“未出生的分支”,这样你就可以在一个没有git commit的分支上实际存在。

这是第一个feat-a创造了另一个未出生的分支。 当你第一次git commit时,你就在master,所以feat-a从未真正创建过,甚至可能在此时停止哎呀,你还没有做出第一次提交,我莫名其妙地想出了一个。所以git commit也未出生,现在仍然是。让我们假装,其余的答案,你确实做了git checkout -b master来创建第一次提交。)

你仍然可以git checkout --orphan master,它将创建它指向当前提交,或--orphan,它将“半创建”它作为未出生的,等待第一次提交。这些之间的区别在于没有--orphan,因为现在有一个现有的提交,git可以并且将在当前提交时完全创建分支。对于Dim randomize As Boolean = False Dim Word(4) As String Dim Definition As String Dim wordNmb As Integer Dim defineNmb As Integer Dim rand As Random Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click randomize = True End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles WordTimer.Tick rand = New Random If randomize = True Then randomize = False defineNmb = rand.Next(1, 5) For x As Integer = 1 To 4 rand = New Random wordNmb = rand.Next(1, 6) Select Case wordNmb Case 1 Word(x) = "Absolve" Definition = "To clear of guilt" Case 2 Word(x) = "Adamant" Definition = "Firm" Case 3 Word(x) = "Amiable" Definition = "Good-natured" Case 4 Word(x) = "Amoral" Definition = "Lacking ethical principles" Case 5 Word(x) = "Animosity" Definition = "Strong dislike" End Select If x = 1 Then If defineNmb = 1 Then pickCorrect.Location = pickWord1.Location Defined.Text = Definition End If Word1.Text = Word(1) ElseIf x = 2 If defineNmb = 2 Then pickCorrect.Location = pickWord1.Location Defined.Text = Definition End If Word2.Text = Word(2) ElseIf x = 3 If defineNmb = 3 Then pickCorrect.Location = pickWord1.Location Defined.Text = Definition End If Word3.Text = Word(3) ElseIf x = 4 If defineNmb = 4 Then pickCorrect.Location = pickWord1.Location Defined.Text = Definition End If Word4.Text = Word(4) End If Next x End If End Sub ,或者在空存储库的特殊情况下,git将不会或不能创建指向现有提交的新分支名称。

答案 1 :(得分:0)

虽然Git的默认分支名为master,但在提交之前,该名称没有任何内容可供指向,因此切换HEAD使用git checkout -b的{​​{1}}别名失去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