我想與您分享我的經驗,與您一起嘗試解決類似問題的Visual Studio解決方案中的外部項目。我對git比較陌生,所以如果任何人有任何建設性的批評,我會很感激。
如果您使用的是Visual Studio,Git源代碼管理提供程序擴展是免費的(http://visualstudiogallery.msdn.microsoft.com/63a7e40d-4d71-4fbb-a23b-d262124b8f4c),並且在我測試它時似乎遞歸提交子模塊。但是我不想依賴擴展(我也認爲很好地瞭解引擎蓋下發生了什麼),但是我在開發中使用VS Web Developer Express。因此我不得不弄清這些命令,並在下面添加了一些註釋。
注意
如果您還沒有這樣做,有一個徹底讀通過http://git-scm.com/book/en/Git-Tools-Submodules。有幾個警告,我會回到這個頁面。如果您嘗試在不閱讀本文的情況下使用子模塊進行操作,您將很快就會頭痛。
我的做法是遵循本教程中,有一些額外設施:http://blog.endpoint.com/2010/04/git-submodule-workflow.html
一旦你有你上層項目初始化(如git init
& & git remote add origin ...
),開始添加你的子模塊,像這樣:
git submodule add git://github.com/you/extension1.git extension
git submodule init
git submodule update
檢查你的.gitmodules文件反映了這一點,例如
[submodule "extension1"]
path = extension
url = git://github.com/you/extension1.git
切換到您的子模塊目錄(即cd extension
)。兼營:
git fetch #I use fetch here - maybe you can use pull?
git checkout -b somebranchname #See the Git-Tools-Submodules link above for an explanation of why you need to branch
我在這裏做了更改的README.txt,所以我可以提交它(也因此我有什麼,我在此承諾做一個記錄),然後COMMITED模塊應用分支(仍然在子模塊目錄):
git add .
git commit -a -m "Branching for extension submodule"
現在進入上層項目(即cd ..
)。您還需要在這裏提交(如果你看看我所提到的git的子模塊頁面也解釋了爲什麼這是必要的):
git status #will show you that your submodule has been modified
git commit -a -m "Commiting submodule changes from superproject"
現在我們可以recusively推動我們的項目,像這樣如果需要的話:
git push --recurse-submodules=on-demand
您需要對所有子模塊執行一次上述步驟。
一旦你完成了這對所有子模塊,並開始製作要提交和推送更改,您可以使用:
git submodule foreach 'git add .' #recursively add files in submodules
Unfortunaly我還沒有找到一種方法,而無需使用類似遞歸提交git-slave
(任何人?),因此您需要進入每個子模塊目錄並對剛剛添加的文件運行常規提交。在上層項目:
git status #tells you that `extension` submodule has been modified
cd extension
git commit -a -m "Commiting extension changes in superproject edit session"
一旦子模塊commiting,您還需要提交上層項目(再次),所以:
cd ..
git add .
git commit -a -m "Altered extension submodule"
git status #should now show 'working directory clean', otherwise commit other submodules in the same manner
這可以讓有點惱人的(因爲你最終提交兩次),但一旦你意識到它實際上並不壞(因爲它迫使你檢查你在每個項目中提交的內容)。只是我的看法 - 如果你已經將一些超級項目的功能分離到了子模塊中,它應該與你的其他項目分開工作(所以在不同時間提交它們,而煩人不是世界的盡頭)。
現在我們可以把一次...
git push --recurse-submodules=on-demand
如果你再進入到您的子模塊並再次嘗試推,你會發現它不會做任何事情的最新提交已是推。
爲超級項目克隆(或使用遠程原點)也可能相當混亂 - 例如需要在git submodule init
後兩次運行git submodule update
。閱讀http://git-scm.com/book/en/Git-Tools-Submodules中的「用子模塊克隆項目」部分。
克隆我的超級項目時引起我注意的事情是獲取子模塊的最新更改。見Easy way pull latest of all submodules
我的這個變體是使用簽出的子模塊一個「發展」分支(但你可以調用它任何你想要的),然後在上層項目使用:
git submodule foreach git pull origin development
當我設置這件事我也交換到我想我的變化推動對檢查出的子模塊,像這樣的分支:
cd extension
git checkout -b development #This will tell you this is a new branch, but I believe this means a new branch of the local git repository - this will get pushed to the 'development' branch
#Make your changes, commit etc.
我可以確認的是,當我按照上面的步驟,在克隆/遠程更改子模塊原始項目(推動時)出現在其他克隆中es /遠程同一項目的起源(不要忘記最後一個子模塊拉命令)。
我希望這對你有一些用處。
是的,當然如果你修改子模塊時沒有提交/推送,當你進行子模塊更新時,它會檢出原子模塊頭,所以你會留下你的修改,爲了不用這種情況,你應該創建一個分支,修改子模塊和提交,如果您進行子模塊更新,您將離開您的分支到原始子模塊頭,不用擔心,您現在可以合併您的子模塊分支或者只需簽出即可。 –
你能更具體嗎?假設兩個程序員正在修改團隊項目的子模塊。他們需要做什麼?它們中的每一個都在子模塊文件夾中創建一個具有相同名稱/修改/提交/推送的分支。和子模塊更新,然後呢? – eugene
我正在嘗試爲您準備更深入的答案,因爲我正在處理類似的問題。與此同時,您是否閱讀過http://blog.endpoint.com/2010/04/git-submodule-workflow.html和http://stackoverflow.com/questions/5814319/git-submodule-push?第一個鏈接還包括用子模塊創建分支(如果您想了解爲什麼閱讀'Submodules問題',請點擊http://git-scm.com/book/en/Git-Tools-Submodules)。 –