今天在工做中遇到一个问题,使用好久的一个local git repository,里面只有develop分支,那么如今想将分支切换到master分支,问题来了,在切换到master分支时:git
git checkout master
提示以下错误:composer
error: pathspec 'master' did not match any file(s) known to git
1.首先咱们看一下分支状况:fetch
git branch -a
* develop remotes/composer/develop remotes/composer/feature/194 remotes/composer/feature/198 remotes/composer/feature/199 remotes/composer/feature/200 remotes/composer/master remotes/origin/HEAD -> origin/develop remotes/origin/develop remotes/origin/feature/194 remotes/origin/feature/198 remotes/origin/feature/199 remotes/origin/feature/200 remotes/origin/master
2.若是没有看到你想要的分支,先获取全部分支:this
git fetch
3.切换到远程master分支:spa
git checkout origin/master
提示以下:code
Note: checking out 'origin/master'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b new_branch_name HEAD is now at 4beea49... Merge branch 'develop' into 'master'
执行git branch,效果以下:orm
* (detached from origin/master) develop
5.如今咱们能够从当前的detached分支切换并新建分支,能够理解为即将新建立的分支是由当前detached 分支出来的(为了为后续作准备,此处新分支就叫作master):ci
git checkout -b master
5.这时咱们使用git pull会提示以下错误:rem
There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details git pull <remote> <branch> If you wish to set tracking information for this branch you can do so with: git branch --set-upstream-to=<remote>/<branch> master
说明咱们新创建的master分支还不能和远程的master分支创建追踪关系(虽然表面咱们看似已经创建了master分支,但git不认为它和远程的master有任何关系),固然,您能够按照上面提示那样,经过git pull指定远程的分支和本地的分支来进行更新,但此处咱们使用提示中的第二种方式,创建本地分支和远程分支的追踪关系:it
git branch -u origin/master master
6.这时咱们执行git pull来看看什么反馈:
Already up-to-date.
总结:其实git的人性化作的很是的完备,有时咱们不要害怕提示,而要从中找到问题的解决方法,并时常利用好:
man git man git branch and so forth!
Bye!