当前位置: 代码迷 >> Ruby/Rails >> 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 )

热度:2216   发布时间:2016-04-29 02:14:52.0
git checkout 报错原因 (error: pathspec 'master' did not match any file(s) known to )
以下是发生的场景
在一个空的目录中(/tempRepo)
执行:
git init
成功创建一个空的git仓库
注:现在tempRepo上除了.git文件夹外,没有任何文件
然后继续创建并打开一个分支(dev)

执行:

git checkout -b dev

现在想checkout 到 master分支
执行:
git checkout master

git checkout -

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

不过这时是报错误的,错误信息如下:

error: pathspec 'master' did not match any file(s) known to git.

这里是因为,还没有文件被提交过。即没有commit 过任何文件。

当commit过以后就可以切换分支了

备注:此时执行:git branch,只显示有dev 这个branch。

不过我们可以直接再创建一个master出来。

======================================================
下面是整个过程


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

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

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

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

[email protected] /E/tRepo (dev)
$ vim readme.txt

[email protected] /E/tRepo (dev)
$ git status
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)
$ git add .

[email protected] /E/tRepo (dev)
$ git status
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)
$ git commit -m "add a new file:readme.txt"
[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)
$ git checkout dev
Already on 'dev'

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

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

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

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

[email protected] /E/tRepo (dev)
$ git checkout -
Switched to branch 'master'
  相关解决方案