2014-09-23 100 views
0

我有3個類似的組織和一些重複的目錄之間的git回購。他們是一個大項目的子項目。Git。如何使共享目錄而不是重複?

Proj1/ 
    .git/ 
    feature1_lib/ 
    feature1_app/ 
    feature1/  <- specific to Proj1 

    feature2_lib/ 
    ... 

Proj2/ 
    .git/ 
    feature1_lib/ 
    feature1_app/ 
    feature1/  <- specific to Proj2 

    feature2_lib/ 
    ... 

Proj3/ 
    .git/ 
    feature1_lib/ 
    feature1_app/ 
    feature1/  <- specific to Proj3 

    feature3_lib/ <- specific to Proj3 
    feature3_app/ <- specific to Proj3 
    feature3/  <- specific to Proj3 
    ... 

它們是同時開發的,但歷史略有不同。

Proj1/ 
     ... 
     commit4 "Commit specific to Proj1." 
     commit5 "Add code to feature1_lib." 
     commit6 "Fix bugs in feature1_lib." 
     commit7 "Refactoring in feature1_lib." 
     ... 

Proj2/ 
     ... 
     commit6 "Import changes from Proj1." <- after commit7 in Proj1 
     commit7 "Commit specific to Proj2." 
     commit8 "Fix bugs in feature2_lib." 
     commit9 "Add code to feature2_lib." 
     ... 

等。

現在我正在尋找切割和粘貼重複部分的選項。我認爲它應該看起來像這樣

Proj1_2_common/ 
    .git/ 
    ... 
Proj1_2_3_common/ 
    .git/ 
    ... 
Proj1/ 
    .git/ 
    ... 
Proj2/ 
    .git/ 
    ... 
Proj3/ 
    .git/ 
    ... 

我讀了關於子樹和子模塊,但沒有完全理解。爲什麼我必須創建子樹,如果我可以將我的項目指向特定目錄?也就是說,我可以簡單地重新配置我的項目以查看"Proj1_2_3_common/feature1_lib/",而不是從"feature1_lib/"製作子樹。

那麼從你的觀點來看,這裏有什麼更好的選擇?什麼是最簡單的?如何處理這種混亂的歷史,在移動之後有一些參考?

回答

2

您應該以其他方式管理應用程序:

  1. 爲每個項目創建一個存儲庫
Proj1/ 
    .git 
Proj2/ 
    .git 
Proj3/ 
    .git 
  • 創建一個存儲庫所有模塊/功能
  • ProjModules/ 
        .git 
        feature1/ 
        feature2/ 
        feature3/ 
    
  • 使用Git的子模塊,包括你的3個項目的功能:
  • Proj1/ 
        .git 
        .gitmodules 
        vendor/ 
        ProjModules/ 
    Proj2/ 
        .git 
        .gitmodules 
        vendor/ 
        ProjModules/ 
    Proj3/ 
        .git 
        .gitmodules 
        vendor/ 
        ProjModules/ 
    

    然後,您可以在Proj1/vendor/ProjModules進行修改和傳播這些變化在Proj2/Proj3/

    $ cd /Proj1/vendor/ProjModules/ 
    $ touch newfile.txt 
    $ git add --all 
    $ git commit -m "New file !!" 
    $ git push origin master 
    
    $ cd ../../../Proj2/vendor/ProjModules/ 
    $ git submodule foreach git pull 
    
    $ cd ../../../Proj3/vendor/ProjModules/ 
    $ git submodule foreach git pull 
    

    另一個想法是創建一個代表每個模塊的ository。這將允許你只導入你需要的。

    +0

    '''我擁有它。 ''我需要從原始回購中刪除它。 '3.爲什麼子模塊在這裏更好?歷史呢? – LVitya 2014-09-23 09:54:18

    +0

    @LVitya通過子模塊,對'*/vendor/ProjModules'所做的更改將受到ProjModules項目歷史的影響。這與如果您切換到「ProjModules」進行更改,然後切換回「Proj *」以提取這些更改的行爲相同。從現在開始,'ProjModules'歷史屬於'ProjModules'項目,'Proj *'歷史屬於'Proj *'項目。 – zessx 2014-09-23 09:58:47

    +0

    與subtree有什麼區別?如何保存以前的分離目錄更改歷史記錄? – LVitya 2014-09-23 11:01:56

    相關問題