文末直接查看解决方法
以下为问题处理过程,以切换到master分支为例
本地只有部分分支,当想切换到其它远程分支时报错:
$ git checkout master
error: pathspec 'master' did not match any file(s) known to git
$ git checkout origin/master
error: pathspec 'origin/master' did not match any file(s) known to git
查看所有分支,发现没有想要切换的分支
$ git branch -a
* dev
remotes/origin/dev
fetch所有分支
git fetch --all Idfetch所有分支
然后git branch -a
发现还是没有想要切换的分支,尝试直接fetch指定分支
$ git fetch master
fatal: 'master' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
提示分支不存在。查看仓库,发现分支并未移除,依然存在。
查看文件 项目路径/.git/config
, 发现remote只添加了单一分支
[remote "origin"]
url = http://XXXXXXXXXXX/XXXXXXXXXXX.git
fetch = +refs/heads/dev:refs/remotes/origin/dev
修改为
[remote "origin"]
url = http://XXXXXXXXXXX/XXXXXXXXXXX.git
fetch = +refs/heads/*:refs/remotes/origin/*
再次fetch分支,成功解决问题
$ git fetch --all
Fetching origin
From http://XXXXXXXXXXX/XXXXXXXXXXX
* [new branch] master -> origin/master
$ git branch -a
* dev
remotes/origin/dev
remotes/origin/master
$ git checkout master
Switched to a new branch 'master'
Branch 'master' set up to track remote branch 'master' from 'origin'.