Git使用之(pathspec master did not match any file(s) known to git)


一 問題概述

今天在工作中遇到一個問題,使用很久的一個local git repository,里面只有develop分支,那么現在想將分支切換到master分支,問題來了,在切換到master分支時:

  1. git checkout master

提示如下錯誤:

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

二 問題解決

1.首先我們看一下分支情況:

  1. git branch -a
  1. * develop
  2.   remotes/composer/develop
  3.   remotes/composer/feature/194
  4.   remotes/composer/feature/198
  5.   remotes/composer/feature/199
  6.   remotes/composer/feature/200
  7.   remotes/composer/master
  8.   remotes/origin/HEAD -> origin/develop
  9.   remotes/origin/develop
  10.   remotes/origin/feature/194
  11.   remotes/origin/feature/198
  12.   remotes/origin/feature/199
  13.   remotes/origin/feature/200
  14.   remotes/origin/master

 

2.如果沒有看到你想要的分支,先獲取所有分支:

  1. git fetch

3.切換到遠程master分支:

  1. git checkout origin/master

 

提示如下:

  1. Note: checking out 'origin/master'.
  2.  
  3. You are in 'detached HEAD' state. You can look around, make experimental
  4. changes and commit them, and you can discard any commits you make in this
  5. state without impacting any branches by performing another checkout.
  6.  
  7. If you want to create a new branch to retain commits you create, you may
  8. do so (now or later) by using -with the checkout command again. Example:
  9.  
  10.   git checkout -b new_branch_name
  11.  
  12. HEAD is now at 4beea49... Merge branch 'develop' into 'master'

 

執行git branch,效果如下:

  1. * (detached from origin/master)
  2.   develop

 

5.現在我們可以從當前的detached分支切換並新建分支,可以理解為即將新創建的分支是由當前detached 分支出來的(為了為后續做准備,此處新分支就叫做master):

  1. git checkout -b master

 

5.這時我們使用git pull會提示如下錯誤:

  1. There is no tracking information for the current branch.
  2. Please specify which branch you want to merge with.
  3. See git-pull(1) for details
  4.  
  5.     git pull <remote> <branch>
  6.  
  7. If you wish to set tracking information for this branch you can do so with:
  8.  
  9.     git branch --set-upstream-to=<remote>/<branch> master

 

說明我們新建立的master分支還不能和遠程的master分支建立追蹤關系(雖然表面我們看似已經建立了master分支,但git不認為它和遠程的master有任何關系),當然,您可以按照上面提示那樣,通過git pull指定遠程的分支和本地的分支來進行更新,但此處我們使用提示中的第二種方式,建立本地分支和遠程分支的追蹤關系:

  1. git branch -u origin/master master

 

6.這時我們執行git pull來看看什么反饋:

  1. Already up-to-date.

 

總結:其實git的人性化做的非常的完備,有時我們不要懼怕提示,而要從中找到問題的解決方法,並時常利用好:

  1. man git
  2. man git branch
  3.  
  4. and so forth!

 

Bye!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM