1. 程式人生 > >git checkout 報錯原因 (error: pathspec 'master' did not match any file(s) known to )

git checkout 報錯原因 (error: pathspec 'master' did not match any file(s) known to )

以下是發生的場景
在一個空的目錄中(/tempRepo)
執行:
[color=violet][b]git init[/b][/color]
成功建立一個空的git倉庫
[color=red][size=xx-small]注:現在tempRepo上除了.git資料夾外,沒有任何檔案
然後繼續建立並開啟一個分支(dev)[/size][/color]
執行:

[color=violet][b]git checkout -b dev[/b][/color]

現在想checkout 到 master分支
執行:
[color=violet][b]git checkout master [/b][/color]

[color=violet][b]git checkout -[/b][/color]

正常情況下是可以回到master分支的

不過這時是報錯誤的,錯誤資訊如下:

[color=red]error: pathspec 'master' did not match any file(s) known to git.[/color]

這裡是因為,還沒有檔案被提交過。即沒有commit 過任何檔案。

當commit過以後就可以切換分支了

備註:此時執行:git branch,只顯示有dev 這個branch。

不過我們可以直接再建立一個master出來。

======================================================
下面是整個過程


[email protected]
/E/tRepo
[color=violet][b]$git init[/b][/color]
Initialized empty Git repository in e:/tRepo/.git/

[email protected] /E/tRepo (master)
[b][color=violet]$ ls -ah[/color][/b]
. .. .git

[email protected] /E/tRepo (master)
[b][color=violet]$ git checkout -b dev[/color][/b]
Switched to a new branch 'dev'

[email protected]
/E/tRepo (dev)
[color=violet][b]$ git checkout master[/b][/color]
[color=red]error: pathspec 'master' did not match any file(s) known to git.[/color]

[email protected] /E/tRepo (dev)
[color=violet][b]$ vim readme.txt[/b][/color]

[email protected] /E/tRepo (dev)
[color=violet][b]$ git status[/b][/color]
On branch dev

Initial commit

Untracked files:
(use "git add <file>..." to include in what will be committed)

readme.txt

nothing added to commit but untracked files present (use "git add" to track

[email protected]
/E/tRepo (dev)
[color=violet][b]$ git add .[/b][/color]

[email protected] /E/tRepo (dev)
[b][color=violet]$ git status[/color][/b]
On branch dev

Initial commit

Changes to be committed:
(use "git rm --cached <file>..." to unstage)

new file: readme.txt


[email protected] /E/tRepo (dev)
[b][color=violet]$ git commit -m "add a new file:readme.txt"[/color][/b]
[dev (root-commit) 06e83d0] add a new file:readme.txt
1 file changed, 1 insertion(+)
create mode 100644 readme.txt

[email protected] /E/tRepo (dev)
[color=violet][b]$ git checkout dev[/b][/color]
Already on 'dev'

[email protected] /E/tRepo (dev)
[color=violet][b]$ git checkout master[/b][/color]
error: pathspec 'master' did not match any file(s) known to git.

[email protected] /E/tRepo (dev)
[b][color=violet]$ git checkout -b master[/color][/b]
Switched to a new branch 'master'

[email protected] /E/tRepo (master)
[b][color=violet]$ git checkout master[/color][/b]
Already on 'master'

[email protected] /E/tRepo (master)
[color=violet][b]$ git checkout dev[/b][/color]
Switched to branch 'dev'

[email protected] /E/tRepo (dev)
[color=violet][b]$ git checkout -[/b][/color]
Switched to branch 'master'