[英]Git: turn a directory with repos into a repo with submodules
我正在“軟件”目錄中開發我的項目。 項目分為子目錄:
software
|
+-- common_forms
| +-- .git
+-- common_utils
| +-- .git
+-- gui_app
| +-- .git
+ ...
... and so on.
每個子目錄是一個 git repo; 主目錄“軟件”不是存儲庫,只是存儲庫的根目錄。
現在我想把主目錄“軟件”變成一個倉庫,並將這些倉庫作為主倉庫的子模塊。 我怎樣才能做到不破壞目錄和回購的現有結構?
讓我在這里添加一個詞——有點多余,但對強調很重要——
每個子目錄已經是一個 git repo; 主目錄“軟件”不是存儲庫,只是存儲庫的根目錄。
在這種情況下,只需進入software
目錄,運行git init
創建一個.git
目錄/存儲庫,以此為頂層工作樹,並使用git submodule add
每個子模塊。 有關 git 子git submodule add
的詳細信息,請參閱git submodule
文檔。 然后git add
.gitmodules
文件並提交,以進行初始提交。
請注意,每個git submodule add
命令的存儲庫參數可以與path
參數相同。 這會影響.gitmodules
文件中的內容,一旦您在剛剛創建的超級項目中進行提交,該文件現在可用於該超級項目的未來克隆。
也就是說,在未來,某人——讓我們給他或她一個名字,例如,帕特——會跑:
git clone $URL [<path>]
從一些 URL 克隆子模塊(現在我假設它在$URL
中,盡管 Pat 可能會輸入文字字符串而不是使用變量)。 然后 Pat 將cd
進入克隆並運行,例如git submodule update --init
。
Pat 這里的克隆需要有git submodule init
或者git submodule update --init
的說明。 這些說明告訴 Git 所有子模塊是什么以及如何git clone
每個子模塊。 That is, rather than Pat having to, now, run one git clone
for each submodule, Pat's git submodule update --init
allows Pat's Git to read, from the .gitmodules
file, the URLs and path names needed for each of these git clone
操作。
一個相對路徑的偽 URL,例如./common_forms
,變成一個相對於 URL 的 URL。 也就是說,由於 Pat 克隆$URL
來創建超級項目,因此.gitmodules
URL ./common_forms
變成了 URL ${URL}/common_forms
。 如果這是正確的 URL,一切都很好。
另一方面,假設您計划將超級項目推送到host1.example.com/supers/superproj.git
但所有子模塊都將位於類似host2.example.com/subs/common_forms.git
這樣的 URL 上。 然后,當您創建.gitmodules
文件時(或至少在提交之前),您應該確保將存儲在.gitmodules
文件中的 URL 是host2.example.com/subs/common_forms
或host2.example.com/subs/common_forms.git
。 這樣,Pat 仍然可以運行git submodule update --init
。
(在可能的情況下,安排存儲庫托管以反映超級項目和子模塊的布局是最有意義的,但並不總是可能的。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.