2015-12-03 121 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