github上 fork + Pull Request 開發模式_git

來源:互聯網
上載者:User
4.1. Fork + Pull模式

參與GitHub中的項目開發,最常用和推薦的首選方式是“Fork + Pull”模式。在“Fork + Pull”模式下,項目參與者不必向項目建立者申請提交許可權,而是在自己的託管空間下建立項目的派生(Fork)。如果一個開源項目派生出另外的項目,通常意味著項目的分裂和Team Dev的削弱,而GitHub中的項目派生則不會,而且正好相反,GitHub中的項目派生是項目壯大的體現。所有的派生項目都會有連結指向原始項目,派生項目沒有獨立的缺陷追蹤系統(ISSUE),而是必須利用建立者本人的項目中的缺陷追蹤系統。至於在派生項目中建立的提交,可以非常方便地利用GitHub的Pull Request工具向原始項目的維護者發送Pull Request。下面以GotGit版本庫為例,介紹如何利用GitHub提供的Fork和Pull Request工具實現工作協同。 4 .1.1. 版本庫派生

GotGit版本庫[1]用於維護《Git權威指南》一書的官網和勘誤,下面示範的勘誤表修改是由王勝[2]通過GitHub之外的一個缺陷追蹤平台報告的[3]。他在報告中,甚至直接用GNU diff格式告訴我該如何修改。下面就以使用者gotgithub身份,訪問版本庫 https://github.com/gotgit/gotgit/ ,添加新的勘誤。如圖4-1所示,gotgit項目在之前的樣本中已經被我們關注但尚未Fork。 點擊項目名稱右側的Fork按鈕,便在gotgithub使用者自己的託管空間下建立項目派生,派生項目版本庫出現在版本庫列表中,如圖4-2。

訪問派生後的版本庫,會發現和派生前的幾乎相同,除了沒有缺陷跟蹤(ISSUE),以及標識了該項目派生之前的原路徑等。如圖4-3所示。

現在gotgithub使用者就在本地派生的版本庫中提交。複製 gotgithub/gotgit 版本庫。

$ git clone git@github.com:gotgithub/gotgit.git$ cd gotgit

為了向問題的發現者致敬,並經王勝同意,以他的身份進行提交。

$ git config user.name "Wang Sheng"$ git config user.email wangsheng@ossxp.com

編輯errata.mkd檔案[4],錄入新發現的書中的文字錯誤。

$ vi errata.mkd

對error.mkd的改動如下:

$ git diffdiff --git a/errata.mkd b/errata.mkdindex b0b68fb..29e40cf 100644--- a/errata.mkd+++ b/errata.mkd@@ -14,5 +14,6 @@ |     66 | 倒數第11行                | Author(提交者)             |  Author(作者)              | [Github#2](http://github.com/gotgit/gotgit/issues/2)    | |    144 | 第1行                     | \`$ **git rev-parse  A^{tree}  A:**  | $ **git rev-parse  A^{tree}  A:**              | [#153](http://redmine.ossxp.com/redmine/issues/153)  | |    218 | 第8行                     | 況下,Gits標識出合并衝突,           | 況下,Git標識出合并衝突,                      | [#159](http://redmine.ossxp.com/redmine/issues/159)  |+|    369 | 第21行                    | 但 `-i` 參數僅當對一個項執行時才有效。 | 但 `-i` 參數僅當對一個項目執行時才有效。     | [Github#3](http://github.com/gotgit/gotgit/issues/3)    | |    516 | 倒數第15行                | **oldtag="cat"**             | **oldtag=\`cat\`**           | [#151](http://redmine.ossxp.com/redmine/issues/151)  | 

提交修改。至於提交說明中出現的編號,是為了和缺陷跟蹤系統關聯,會在後面章節介紹。

$ git add -u$ git commit -m "Fixed #3: should be 項目, not 項."

推送提交到GitHub。

$ git push

訪問GitHub上的派生項目頁面,會看到以使用者whangsheng在master分支[5]建立的提交。如圖4-4所示。

4.1.2. Pull Request

那麼如何能夠讓gotgit原始項目的建立者知道這個派生項目及新的提交呢。GitHub提供的工具就是“Pull Request”。注意到圖4-3右上方“Pull Request”按鈕了麼。點擊該按鈕進入Pull Request建立介面。在彈出的Pull Request建立介面中,點擊菜單中的“Commits”,查看所包含的提交。如圖4-5所示。

點擊菜單中的“Files Changed”,查看所包含的提交。如圖4-6所示。

點擊菜單中的“Preview Discussion”,填寫Pull Request的標題和內容,完成Pull Request的建立。如圖4-7所示。

當Pull Request發出後,項目gotgit的開發人員會收到通知訊息,如圖4-8所示。

點擊郵件中的URL連結,以項目gotgit的開發人員(如ossxp-com)身份登入,看到如圖4-9的視圖。之所以看到有兩個使用者參與到此Pull Request,是因為Pull Request建立者和提交的作者是不同的使用者。圖4-9下方的表單可以向Pull Request追加評論,或者關閉此Pull Request。

GitHub如果檢測到Pull Request中包含的提交可以直接合并,會顯示自動合并的提示資訊,點擊圖4-9中提示資訊中的自動合并按鈕,顯示圖4-10的自動合併交談框。

點擊“Confirm Merge”按鈕即完成Pull Request中所含提交的自動合并。自動合并完成後,Pull Request頁面下方會以評論的形式出現相關提示,並自動關閉Pull Request。如圖4-11所示。 4.1.3. 手工合并

Pull Request提供的自動合并顯示在提交日誌中是什麼樣子的呢。以使用者ossxp-com身份檢出版本庫,會看到使用者wangsheng的提交已經合并到版本庫中。

$ git clone git@github.com:gotgit/gotgit.git$ cd gotgit$ git log --graph -3*   commit 6c1f1ee152629fd2f8d00ebe92c27a32d068d756|\  Merge: 00c6c4b 7ecdfe7| | Author: OpenSourceXpress <worldhello.net@gmail.com>| | Date:   Tue Aug 16 01:23:47 2011 -0700| || |     Merge pull request #4 from gotgithub/master| || |     Find a typo in the book| || * commit 7ecdfe7451412cfb2e65bb47c12cf2162e21c841|/  Author: Wang Sheng <wangsheng@ossxp.com>|   Date:   Tue Aug 16 10:17:53 2011 +0800||       Fixed #3: should be 項目, not 項.|* commit 00c6c4bfab9824bd967440902ce87440f9e87852| Author: Jiang Xin <worldhello.net@gmail.com>| Date:   Wed Aug 3 11:50:31 2011 +0800||     Change font color for stronger text from red to brown.

可以看出GitHub的自動合并產生了一個合并提交,類似執行git merge --no-ff命令。也就是說即使使用者wangsheng的提交是一個“快進式提交”(基於gotgit/gotgit版本庫最新提交所做的提交),也要產生一個合并提交。可能有人並不喜歡這種用–no-ff參數的非標準的合并方式,因為這種合并產生了一個多餘的提交,可能增加程式碼檢閱的負擔。若要取消GitHub的自動合并也很簡單,因為Git無所不能:

$ git reset --hard HEAD^  # 回退一個提交,即回退到當前提交的第一個父提交$ git rev-parse HEAD      # 檢查是否正確的回退00c6c4bfab9824bd967440902ce87440f9e87852$ git push -f             # 強制推送回退的 master 分支

下面就示範一下當收到他人的Pull Request後,該如何手動合并。實際上在很多情況下,Pull Request所含提交有可能造成合并衝突,那樣的話GitHub不再、也不能提供自動合并功能,就必須採用手工合并的方式。將Pull Request發出者的派生版本庫添加為一個新的源。例如收到來自gotgithub使用者的Pull Request,不妨以gotgithub為名添加新的源。

$ git remote add gotgithub https://github.com/gotgithub/gotgit.git

此時版本庫中有兩個源,一個複製時自動建立的origin,另外一個就是新增加的gotgithub。

$ git remote -vgotgithub       https://github.com/gotgithub/gotgit.git (fetch)gotgithub       https://github.com/gotgithub/gotgit.git (push)origin  git@github.com:gotgit/gotgit.git (fetch)origin  git@github.com:gotgit/gotgit.git (push)

擷取遠程版本庫gotgithub的分支和提交。

$ git fetch gotgithubFrom https://github.com/gotgithub/gotgit * [new branch]      gh-pages   -> gotgithub/gh-pages * [new branch]      master     -> gotgithub/master 

現在除了本地分支master外,還有若干遠程分支,如下:

$ git branch -a* master remotes/gotgithub/gh-pages remotes/gotgithub/master remotes/origin/HEAD -> origin/master remotes/origin/gh-pages remotes/origin/master

將遠程分支remotes/gotgithub/master(可簡寫為gotgithub/master)合并到當前分支中。

$ git merge gotgithub/masterUpdating 00c6c4b..7ecdfe7Fast-forwarderrata.mkd |    1 +1 files changed, 1 insertions(+), 0 deletions(-)

查看提交說明,看到此次合并沒有產生不必要的合并提交。

$ git log --graph -2* commit 7ecdfe7451412cfb2e65bb47c12cf2162e21c841| Author: Wang Sheng <wangsheng@ossxp.com>| Date:   Tue Aug 16 10:17:53 2011 +0800||     Fixed #3: should be 項目, not 項.|* commit 00c6c4bfab9824bd967440902ce87440f9e87852| Author: Jiang Xin <worldhello.net@gmail.com>| Date:   Wed Aug 3 11:50:31 2011 +0800||     Change font color for stronger text from red to brown.

將合并推送到GitHub版本庫中。

$ git push
4.1.4. 線上編輯

GitHub提供了線上編輯功能,這樣可以無需複製版本庫、無需使用Git即可完成對版本庫中檔案的修改,甚至可以在你的iPad甚至iPhone上完成對檔案的修改。以gotgithub賬戶身份登入GitHub,訪問之前派生而來的版本庫gotgithub/gotgit中的檔案,例如檔案errata.md[6],會看到其中一個“Edit this file”的按鈕,如圖4-12所示。

點擊圖4-12中的“Edit this file”按鈕,開始線上編輯檔案errata.md,編輯器還支援文法加亮,如圖4-13所示。 4.1.5. 簡化的 Fork + Pull Request

到目前,我們已經瞭解了GitHub的三大武器:Fork、Pull Request和線上編輯。對於最常用的“Fork + Pull Request”操作,GitHub還提供了一個快捷模式。即GitHub對於無權更改的他人版本庫中的檔案,提供了一個類似線上編輯的按鈕,名為“Fork and edit this file”按鈕,自動完成版本庫派生和線上編輯,即將三大武器一勺燴。訪問他人版本庫(尚未在自己空間派生)中的檔案,例如訪問下面地址:http://git.io/hello-world-makefile[7]。顯示他人(ossxp-com)版本庫hello-world中的src/Makefile檔案,如圖4-14所示。

點擊圖4-14中的“Fork and edit this file”按鈕,會自動在自己託管空間建立派生版本庫,並開始線上編輯檔案src/Makefile,如圖4-15所示。

檔案修改完畢,點擊“Propose File Change”按鈕,會將改動作提交到派生的版本庫中,並馬上開啟一個新的Pull Request。如圖4-16所示。

點擊“Send pull request”按鈕完成Pull Request的建立。如果仔細查看圖4-16,會發現Pull Request所包含的修改發生在gotgithub/hello-world派生版本庫中的patch-1分支中,並非通常的master分支。原版本庫ossxp-com/hello-world的開發人員會收到一封郵件,通知有新的Pull Request,如下所示(前四行為信頭):

From: GotGitHub <reply+i-...@reply.github.com>Date: 2011/12/17Subject: [hello-world] Bugfix: build target when version.h changed.  (#1)To: Jiang Xin <worldhello.net@gmail.com>Without this fix, when version changed only version.h update, target rebuild needs a second `make`.You can merge this Pull Request by running: git pull https://github.com/gotgithub/hello-world patch-1Or you can view, comment on it, or merge it online at: https://github.com/ossxp-com/hello-world/pull/1-- Commit Summary --* Bugfix: build target when version.h changed.-- File Changes --M src/Makefile (3)-- Patch Links -- https://github.com/ossxp-com/hello-world/pull/1.patch https://github.com/ossxp-com/hello-world/pull/1.diff---Reply to this email directly or view it on GitHub:https://github.com/ossxp-com/hello-world/pull/1

版本庫ossxp-com/hello-world的管理員既可以通過GitHub提供的圖形化介面完成對 Pull Request 的審核和合并,也可以在命令列下完成。正如郵件中所述若使用命令列,操作如下:

$ git pull https://github.com/gotgithub/hello-world patch-1

[1] https://github.com/gotgit/gotgit/[2] https://github.com/wangsheng/[3] http://redmine.ossxp.com/redmine/issues/161[4] 版本庫 gotgit/gotgit 已將勘誤檔案重新命名為errata.md。[5] 版本庫 gotgit/gotgit 原master分支內容已轉移至gh-pages分支,通過GitHub提供的網站部署機制完成網頁的編譯和部署。[6] 版本庫 gotgit/gotgit 已重構。分支gh-pages中檔案errata.md檔案來自於原master分支的errata.mkd檔案,地址:https://github.com/gotgithub/gotgit/blob/gh-pages/errata.md 。[7] 即地址 https://github.com/ossxp-com/hello-world/blob/master/src/Makefile 。

轉自:http://www.worldhello.net/gotgithub/04-work-with-others/010-fork-and-pull.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.