2012-08-22 94 views
6

我們的Git存儲庫最近出現了很多問題。我們是git子模塊的用戶,在我們的應用程序之間共有4個共享存儲庫。Git子模塊工作流程問題

例如,資料庫「網站​​」總共有3個子模塊。

[submodule "vendor/api"] 
    path = vendor/api 
    url = [email protected]:api 
[submodule "vendor/auth"] 
    path = vendor/auth 
    url = [email protected]:auth 
[submodule "vendor/tools"] 
    path = vendor/tools 
    url = [email protected]:tools 

我們已經正確簽出了我們的主存儲庫「網站」。現在我的同事一個已經做了一推,然後我git pull; git status

# On branch master 
# Changed but not updated: 
# (use "git add <file>..." to update what will be committed) 
# (use "git checkout -- <file>..." to discard changes in working directory) 
# 
# modified: vendor/api (new commits) 
# modified: vendor/auth (new commits) 
# modified: vendor/tools (new commits) 
# 
no changes added to commit (use "git add" and/or "git commit -a") 

[email protected]:~/projects/website$ git diff 

diff --git a/vendor/api b/vendor/api 
index 41795fc..b582d80 160000 
--- a/vendor/api 
+++ b/vendor/api 
@@ -1 +1 @@ 
-Subproject commit 41795fc4dde464d633f4c0f01eebb6ab1ad55582 
+Subproject commit b582d802419b0ee7bc3959e7623fec0b94680269 
diff --git a/vendor/auth b/vendor/auth 
index a00369b..4599a71 160000 
--- a/vendor/auth 
+++ b/vendor/auth 
@@ -1 +1 @@ 
-Subproject commit a00369bf29f14c761ce71f7b95aa1e9c107fb2ed 
+Subproject commit 4599a7179c9b7ca4afa610a15ffa4a8fc6ebf911 
diff --git a/vendor/tools b/vendor/tools 
index f966744..c678cf6 160000 
--- a/vendor/tools 
+++ b/vendor/tools 
@@ -1 +1 @@ 
-Subproject commit f966744359510656b492ae3091288664cdb1410b 
+Subproject commit c678cf6f599fc450e312f0459ffe74e593f5890f 

什麼是與git diff這個問題?問題是每個子模塊的新提交比舊的提交更舊,而不是將被覆蓋的提交。這不是我們想要的,因爲在存儲庫正確指向41795fc4dde464d633f4c0f01eebb6ab1ad55582,a00369bf29f14c761ce71f7b95aa1e9c107fb2edf966744359510656b492ae3091288664cdb1410b,如果我們將這些修改添加到我們的下一個提交中,我們可能會制動事情。我不知道爲什麼它得到最舊的版本,而不是最新版本。

我試圖通過我自己,但沒有成功,以解決這個問題:

[email protected]:~/projects/website$ git pull; git submodule foreach git pull 

做着最後的命令,這不是正確的,因爲我們將「網站」的指針可能更新到最新的每個子模塊中,我們不想要這個。我們希望保留它在存儲庫上的正確版本。

其中之一,我已經解釋說,我們通常這裏面的子模塊的工作,例如事情:

[email protected]:~/projects/website$ cd vendor/api 
[email protected]:~/projects/website/vendor/api$ git checkout master 
[email protected]:~/projects/website/vendor/api$ echo "lorem ipsum" >> example.file 
[email protected]:~/projects/website/vendor/api$ git add example.file; git push 

當我們做一個git submodule update「主」分支是失去了對每個子模塊。

最後,什麼對做pushpull並與子模塊工作,沒有這一切問題的正確方法是什麼?

預先感謝您

回答

8

看看的git-scm documention,並通過它給你的團隊。您所看到的現象在"Cloning a Project with Submodules"部分有詳細說明。

首先,您觀察到的初始狀態,其中git diff對於那些提交散列顯示意外相反的結果,表示您在父回購中合併了子模塊更新,但未在本地運行git submodule update。每次在主項目中下拉子模塊更改時,必須運行git submodule update。爲什麼?子模塊的指針(即父存儲庫認爲是vendor/auth的狀態)實際上並不是子模塊存儲庫vendor/auth的落實HEAD。直到你瞭解git如何跟蹤子模塊狀態,這有點令人困惑。再次,git-scm documentation值得一讀。

二,git submodule update設計上放棄子模塊上的master分支。查看這些文檔的"Issues with submodules"部分。該名男子頁,這是常有用git真實的,告訴我們什麼,我們需要知道:

update 
    Update the registered submodules, i.e. clone missing submodules and checkout the commit specified in the index of the containing repository. This will 
    make the submodules HEAD be detached unless --rebase or --merge is specified or the key submodule.$name.update is set to rebase, merge or none. none 
    can be overridden by specifying --checkout. 

你每次發出git submodule update時間把你的子模塊中的「超然HEAD」狀態,沒有一個參數。

那麼如何在不存在這些問題的情況下使用子模塊?首先,問問你自己和你的團隊:我們真的需要他們嗎?子模塊在某些情況下是一個強大且有用的功能,但它們設計的更多的是第三方庫,而不是將活動項目分解爲子庫。你當然可以這樣使用它們,但管理開銷可能會迅速超過你獲得的任何好處。除非您的存儲庫很大,或者您的子模塊完全是模塊化的,否則值得詢問"Would we be better off with a single repository?"即使答案是「否」,也可以查看subtree merging,,這對您的使用情況可能會更成功。

如果您仍希望使用子模塊,請查看上面鏈接的the docs以及SO和其他站點上有關子模塊工作流程的許多questionsanswers。他們應該幫助你實現一個更加安全的過程。