?
This document uses PHP Chinese website manual Release
gitsubmodules - 在另一個(gè)倉(cāng)庫(kù)中安裝一個(gè)倉(cāng)庫(kù)
.gitmodules, $GIT_DIR/config
git submodule git <command> --recurse-submodules
子模塊是嵌入在另一個(gè)存儲(chǔ)庫(kù)內(nèi)的存儲(chǔ)庫(kù)。子模塊有其自己的歷史; 它所嵌入的存儲(chǔ)庫(kù)稱為超級(jí)項(xiàng)目。
在文件系統(tǒng)上,子模塊通常(但并不總是 - 見下面的表格)由(i)位于$GIT_DIR/modules/
其超級(jí)項(xiàng)目目錄下的 Git 目錄,(ii)超級(jí)項(xiàng)目工作目錄內(nèi)的工作目錄以及.git
根目錄下的文件指向(i)的子模塊工作目錄。
假設(shè)子模塊在$GIT_DIR/modules/foo/
一個(gè)工作目錄下有一個(gè)Git目錄path/to/bar/
,那么超級(jí)gitlink
項(xiàng)目通過表單的樹中的path/to/bar
一個(gè)入口和其.gitmodules
文件中的一個(gè)入口(請(qǐng)參閱gitmodules [5])來跟蹤子模塊submodule.foo.path = path/to/bar
。
該gitlink
條目包含超級(jí)項(xiàng)目期望子模塊的工作目錄所處的提交的對(duì)象名稱。
所述部分submodule.foo.*
中.gitmodules
的文件提供了額外的提示來GITS瓷層,諸如在哪里獲得經(jīng)由所述子模塊submodule.foo.url
的設(shè)置。
子模塊可以用于至少兩種不同的使用情況:
使用另一個(gè)項(xiàng)目同時(shí)保持獨(dú)立的歷史 子模塊允許您在自己的工作樹中包含另一個(gè)項(xiàng)目的工作樹,同時(shí)保持兩個(gè)項(xiàng)目的歷史分離。另外,由于子模塊固定為任意版本,所以其他項(xiàng)目可以獨(dú)立開發(fā)而不會(huì)影響超級(jí)項(xiàng)目,只有在需要時(shí)才允許超級(jí)項(xiàng)目項(xiàng)目自行修復(fù)新版本。
將一個(gè)(邏輯上單一的)項(xiàng)目拆分成多個(gè)存儲(chǔ)庫(kù)并將它們重新綁定在一起。這可以用來克服 Gits 實(shí)現(xiàn)的當(dāng)前局限性,以實(shí)現(xiàn)更好的粒度訪問:
- Size of the git repository: In its current form Git scales up poorly for large repositories containing content that is not compressed by delta computation between trees. However you can also use submodules to e.g. hold large binary assets and these repositories are then shallowly cloned such that you do not have a large history locally.
- Transfer size: In its current form Git requires the whole working tree present. It does not allow partial trees to be transferred in fetch or clone.
- Access control: By restricting user access to submodules, this can be used to implement read/write policies for different users.
子模塊操作可以使用以下機(jī)制進(jìn)行配置(從最高到最低優(yōu)先級(jí)):
支持獲取子模塊規(guī)格的那些命令的命令行。大多數(shù)命令都有一個(gè)布爾標(biāo)志,--recurse-submodules
是否遞歸到子模塊中。例子是ls-files
或checkout
。一些命令使用枚舉,比如fetch
和push
,您可以指定子模塊如何受到影響。
子模塊內(nèi)部的配置。這包括$GIT_DIR/config
在子模塊中,還包括樹中的設(shè)置(如a .gitattributes
或.gitignore
指定子模塊內(nèi)命令行為的文件)。例如,.gitignore
當(dāng)您git status --ignore-submodules=none
在超級(jí)項(xiàng)目中運(yùn)行時(shí),將會(huì)觀察到子模塊文件的影響。這通過status
在注意其.gitignore
文件的子模塊中運(yùn)行來從子模塊的工作目錄收集信息。子模塊的$GIT_DIR/config
文件git push --recurse-submodules=check
在超級(jí)項(xiàng)目中運(yùn)行時(shí)會(huì)起作用,因?yàn)檫@將檢查子模塊是否有任何未發(fā)布到任何遠(yuǎn)程的更改。遙控器在$GIT_DIR/config
文件中像往常一樣在子模塊中進(jìn)行配置。
$GIT_DIR/config
超級(jí)項(xiàng)目中的配置文件。在這個(gè)地方的典型配置是控制一個(gè)子模塊是否通過active
例如標(biāo)志遞歸到所有的子模塊中。如果子模塊尚未初始化,那么子模塊內(nèi)部的配置尚不存在,因此例如在此處配置從哪里獲取子模塊的配置。
.gitmodules
超級(jí)項(xiàng)目中的文件。除了子模塊名稱和路徑之間所需的映射之外,項(xiàng)目通常使用此文件為上游的存儲(chǔ)庫(kù)集合建議缺省值。該文件主要用作超級(jí)項(xiàng)目中名稱和路徑之間的映射,以便可以找到子模塊的git目錄。如果子模塊從未初始化,則這是唯一可以找到子模塊配置的地方。它作為最后一個(gè)回退來指定從哪里獲取子模塊。
子模塊可以采取以下形式:
描述中描述的基本形式包含 Git 目錄,工作目錄,gitlink
一個(gè).gitmodules
條目和一個(gè)條目。
“舊式”子模塊:帶有嵌入式.git
目錄的工作目錄,以及超級(jí)項(xiàng)目中的跟蹤gitlink
和.gitmodules
條目。這通常在使用較舊版本的 Git 生成的存儲(chǔ)庫(kù)中找到??梢允謩?dòng)構(gòu)建這些舊的表單存儲(chǔ)庫(kù)。在取消初始化或刪除(見下文)時(shí),子模塊 Git 目錄會(huì)自動(dòng)移動(dòng)到$GIT_DIR/modules/<name>/
超級(jí)項(xiàng)目。
去初始化子模塊:A gitlink
和一個(gè).gitmodules
條目,但沒有子模塊工作目錄。子模塊的git目錄可能在那里,因?yàn)樵诔跏蓟痝it目錄后會(huì)一直存在。應(yīng)該是工作目錄的目錄是空的。子模塊可以通過運(yùn)行取消初始化git submodule deinit
。除了清空工作目錄外,該命令只修改超級(jí)項(xiàng)目的$GIT_DIR/config
文件,所以超級(jí)項(xiàng)目的歷史不受影響。這可以撤消使用git submodule init
。
已刪除的子模塊:可以通過運(yùn)行刪除子模塊git rm <submodule path> && git commit
。這可以撤消使用git revert
。刪除操作將刪除超級(jí)項(xiàng)目的跟蹤數(shù)據(jù),這些數(shù)據(jù)既是gitlink
條目又是.gitmodules
文件中的部分。子模塊的工作目錄已從文件系統(tǒng)中刪除,但Git目錄保留在其中,以便可以檢出過去的提交而無需從其他存儲(chǔ)庫(kù)中提取。要完全刪除子模塊,請(qǐng)手動(dòng)刪除$GIT_DIR/modules/<name>/
。
# add a submodule git submodule add <url> <path>
# occasionally update the submodule to a new version:git -C <path> checkout <new version>git add <path>git commit -m "update submodule to new version"
# See the list of submodules in a superproject git submodule status
# See FORMS on removing submodules
# Enable recursion for relevant commands, such that # regular commands recurse into submodules by defaultgit config --global submodule.recurse true
# Unlike the other commands below clone still needs # its own recurse flag:git clone --recurse <URL> <directory>cd <directory>
# Get to know the code:git grep foo git ls-files
# Get new codegit fetch git pull --rebase
# change worktree git checkout git reset
克隆或拉取包含子模塊的存儲(chǔ)庫(kù)時(shí),默認(rèn)情況下不會(huì)檢出子模塊; 您可以指示clone
遞歸到子模塊。該init
和update
的子命令git submodule
將保持子模塊簽出,并在您的工作樹適當(dāng)?shù)男抻??;蛘?,您可以設(shè)置submodule.recurse
為checkout
遞歸到子模塊。