一 問題概述
今天在工作中遇到一個問題,使用很久的一個local git repository,里面只有develop分支,那么現在想將分支切換到master分支,問題來了,在切換到master分支時:
- git checkout master
提示如下錯誤:
- error: pathspec 'master' did not match any file(s) known to git
二 問題解決
1.首先我們看一下分支情況:
- 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.如果沒有看到你想要的分支,先獲取所有分支:
- git fetch
3.切換到遠程master分支:
- git checkout origin/master
提示如下:
- 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,效果如下:
- * (detached from origin/master)
- develop
5.現在我們可以從當前的detached分支切換並新建分支,可以理解為即將新創建的分支是由當前detached 分支出來的(為了為后續做准備,此處新分支就叫做master):
- git checkout -b master
5.這時我們使用git pull會提示如下錯誤:
- 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指定遠程的分支和本地的分支來進行更新,但此處我們使用提示中的第二種方式,建立本地分支和遠程分支的追蹤關系:
- git branch -u origin/master master
6.這時我們執行git pull來看看什么反饋:
- Already up-to-date.
總結:其實git的人性化做的非常的完備,有時我們不要懼怕提示,而要從中找到問題的解決方法,並時常利用好:
- man git
- man git branch
- and so forth!
Bye!