fetch 与引用规格(refspec)—— Git 学习笔记 23
fetch 與引用規格(refspec)
本文想討論 fetch 命令的細節。
假設我們用命令添加了一個遠程版本庫:
$ git remote add origin https://github.com/schacon/simplegit-progit上述命令會在 .git/config 文件中添加幾行,并在其中指定遠程版本庫名稱(origin)、URL 和一個用于獲取(fetch)操作的引用規格(refspec):
[remote "origin"]url = https://github.com/schacon/simplegit-progitfetch = +refs/heads/*:refs/remotes/origin/*第1行:表示遠程倉庫的簡稱是 origin;
第2行:指明遠程倉庫的 URL;
第3行:引用規格的格式由一個可選的 + 號和緊隨其后的 <src>:<dst> 組成。其中
- <src> 是一個模式(pattern),代表遠程版本庫中的引用;
- <dst> 是遠程版本庫的引用在本地所對應的位置;
- 開頭的 + 號告訴 Git 即使在不能快進的情況下也要(強制)更新引用。
默認情況下,引用規格由 git remote add 命令自動生成, Git 會獲取服務器中 refs/heads/ 下面的所有引用,并將它寫入到本地的 refs/remotes/origin/ 中。
如果運行git fetch 命令,示意圖如下(命令中的灰色部分是默認參數):
所以,如果服務器上有一個 master 分支,我們可以在本地通過下面這種方式來訪問該分支上的提交記錄:
$ git log origin/master $ git log remotes/origin/master $ git log refs/remotes/origin/master上面的三個命令作用相同,因為 Git 會把它們都擴展成 refs/remotes/origin/master。
如果想讓 Git 每次只拉取遠程的 master 分支,而不是所有分支,可以把上文引用規格的第 3 行修改為:
fetch = +refs/heads/master:refs/remotes/origin/master這也是針對遠程版本庫 origin 的 git fetch 操作的默認引用規格。
對于那些只執行一次的 fetch 操作,我們可以在命令行指定引用規格。 比如,只想將遠程的 master 分支抓取到本地的 origin/master 分支,可以運行:
$ git fetch origin master:refs/remotes/origin/master也可以在命令行中按照如下的方式抓取多個分支:
$ git fetch origin master:refs/remotes/origin/master \topic:refs/remotes/origin/topic From git@github.com:schacon/simplegit! [rejected] master -> origin/master (non fast forward)* [new branch] topic -> origin/topic在這個例子中,對 master 分支的抓取操作被拒絕,因為它不是一個可快進的引用。 可以在引用規格之前指定 + 號來強制抓取。例如:
$ git fetch origin +master:refs/remotes/origin/master \topic:refs/remotes/origin/topic上面的命令表示,對于遠程倉庫的 master 分支,會強制抓取到本地,但是對于 topic 分支僅允許快進式抓取。
你也可以在配置文件中指定多個用于 fetch 操作的引用規格。 如果想在每次抓取時都包括 master 和 experiment 分支,可以這樣寫:
[remote "origin"]url = https://github.com/schacon/simplegit-progitfetch = +refs/heads/master:refs/remotes/origin/masterfetch = +refs/heads/experiment:refs/remotes/origin/experiment參考資料
【1】 https://git-scm.com/book/zh/v2/
【2】《Git 高手之路》,人民郵電出版社
總結
以上是生活随笔為你收集整理的fetch 与引用规格(refspec)—— Git 学习笔记 23的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: es文本分析java代码_Elastic
- 下一篇: ubuntu测试键盘工具_强势霸榜Git