2010-12-21 279 views
29

我有一個主要的git存儲庫A,我們正在使用另一個git存儲庫B在我們的主項目的子目錄中的源。 現在,在此使用的子目錄中的A存儲庫中檢出B存儲庫會很好。 如果其他人然後克隆存儲庫,當然他應該得到我們的主存儲庫A並且在那裏自動存儲B存儲庫。Git存儲庫內的Git存儲庫

讓我想象的目錄結構:

 
+ main_repository  - the root directory of the main Repository 
    + src     - directory containing the source 
    + foreignRepo  - this should be the root directory of another git repo 
    + binaries 
    + other 

這必須在遠程存儲庫中也稱,只是一個本地副本並不能幫助我,因爲其他人檢查了這一點,並且必須能夠編譯所有的東西。

+4

你應該看看你的需求的Git子模塊:http://book.git-scm.com/5_submodules.html – 2010-12-21 14:32:04

+1

更新後的鏈接:http://schacon.github.io/gitbook/5_submodules.html – twasbrillig 2014-11-14 05:20:59

+0

博客post解釋了每種方法的優缺點[Git Submodule:Git Subtree](http://blogs.atlassian.com/2013/05/alternatives-to-git-submodule-git-subtree/)。 – 2016-02-16 13:34:23

回答

22

你會想要閱讀約Git submodules

+1

或者從Pro Git書籍:http://progit.org/book/ch6-6.html – bdukes 2010-12-21 15:18:50

5

你可以在不使用子模塊的情況下嵌套兩個git倉庫。假設ChildRepo是ParentRepo的子目錄,並且都是git存儲庫。

+ ParentRepo 
    - ChildRepo 

如果您將文件添加到ChildRepo,ParentRepo將忽略它。當你提交它時,它會被添加到ChildRepo。無法在ChildRepo內向ParentRepo添加文件。

更多信息:Nested GIT repo gotchas!

5

使用git subtree在Git版本1.7.11及以上。 git subtree is superior to git submodule因爲:

  • 管理簡單的工作流程很簡單。混蛋舊版本的 支持(甚至在V1.5.2)
  • 子項目的代碼是超級項目的克隆完成
  • git subtree後並不需要你的資料庫的用戶學習任何新的可用的權利,他們可以忽略您正在使用的子樹來管理依賴
  • 事實
  • git subtreegit submodule做(如 .gitmodule
  • 子樹的內容可以修改不增加新的元數據文件,而無需的一個獨立的存儲設備拷貝依賴於其他地方

此外,如果您需要detach a subtree of your existing repository into a new repository,git subtree split可以幫助你。