簡體   English   中英

Git:將帶有 repos 的目錄變成帶有子模塊的 repo

[英]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_formshost2.example.com/subs/common_forms.git 這樣,Pat 仍然可以運行git submodule update --init

(在可能的情況下,安排存儲庫托管以反映超級項目和子模塊的布局是最有意義的,但並不總是可能的。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM