2010-12-07 78 views
23

如何鏈接1個倉庫到其他倉庫?如何將1個倉庫鏈接到其他倉庫?

假設我有以下庫:

/var/Common.git

/var/Project1.git

/var/Project2.git

現在,我想在其他存儲庫中使用Common.git。我該怎麼做 ?

+0

https://git.wiki.kernel.org/index.php/GitSubmoduleTutorial – 2010-12-07 15:17:35

回答

37

你可能尋找submodules

子模塊允許外國庫被嵌入源代碼樹的專用子目錄中,總是指着一個特定的提交。

一個關鍵的字有嵌入式:Common.git的實際克隆將被嵌入到其他各個項目。這通常適用於當您不打算在其他項目中修改它時,只使用一個版本,並立即從原始的Common.git更新該版本。你會做這樣的事情:

# add Common.git as a submodule at the path "common" inside this repo 
git submodule add /var/Common.git common 
# initialize it, clone, and check out a copy 
git submodule update --init 
# commit the addition of the submodule 
git commit 

注意,路徑子模塊將被提交到存儲庫,所以你應該使用一個公開可用的網址。如果您想在本地自定義它,您可以運行git submodule init,在.git/config中編輯url,然後運行git submodule update。如果您還有其他問題,請查閱手冊頁或搜索SO;這裏有很多子模塊的問題。

另一方面,如果您要編輯每個項目中的Common.git的內容,您可能需要使用git-subtree,它是git的子樹合併功能的友好包裝。這可以讓你將common.git的內容視爲每個項目中的跟蹤內容,同時仍然可以將提交的內容分解併合併到Common.git本身,並將更新合併到Common.git中並重新合併到項目中。

4

這是git submodule爲之設計的一個完美案例:http://git-scm.com/docs/git-submodule

內PROJECT1和Project2的,你加常見的子模塊。然後你git submodule checkout

在克隆回購它只存儲公共git的散列。所以你git submodule init和結帳。

相關問題