一 問題概述
今天在工做中遇到一個問題,使用好久的一個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
提示以下:orm
- 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,效果以下:ci
- * (detached from origin/master)
- develop
5.如今咱們能夠從當前的detached分支切換並新建分支,能夠理解爲即將新建立的分支是由當前detached 分支出來的(爲了爲後續作準備,此處新分支就叫作master):rem
- git checkout -b master
5.這時咱們使用git pull會提示以下錯誤:it
- 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指定遠程的分支和本地的分支來進行更新,但此處咱們使用提示中的第二種方式,創建本地分支和遠程分支的追蹤關係:io
- git branch -u origin/master master
6.這時咱們執行git pull來看看什麼反饋:
- Already up-to-date.
總結:其實git的人性化作的很是的完備,有時咱們不要害怕提示,而要從中找到問題的解決方法,並時常利用好:
- man git
- man git branch
-
- and so forth!
Bye!