添加
git submodule add <url> <path>
- url:替換為自己要引入的子模塊倉庫地址
- path:要存放的本地路徑
執行添加命令成功后,可以在當前路徑中看到一個.gitsubmodule文件,里面的內容就是我們剛剛add的內容
如果在添加子模塊的時候想要指定分支,可以利用 -b 參數
git submodule add -b <branch> <url> <path>
例子
未指定分支
git submodule add https://github.com/tensorflow/benchmarks.git 3rdparty/benchmarks
.gitsubmodule內容
[submodule "3rdparty/benchmarks"]
path = 3rdparty/benchmarks
url = https://github.com/tensorflow/benchmarks.git
指定分支
git submodule add -b cnn_tf_v1.10_compatible https://github.com/tensorflow/benchmarks.git 3rdparty/benchmarks
.gitsubmodule內容
[submodule "3rdparty/benchmarks"]
path = 3rdparty/benchmarks
url = https://github.com/tensorflow/benchmarks.git
branch = cnn_tf_v1.10_compatible
使用
當我們add子模塊之后,會發現文件夾下沒有任何內容。這個時候我們需要再執行下面的指令添加源碼。
git submodule update --init --recursive
這個命令是下面兩條命令的合並版本
git submodule init
git submodule update
更新
我們引入了別人的倉庫之后,如果該倉庫作者進行了更新,我們需要手動進行更新。即進入子模塊后,執行
git pull
進行更新。
刪除
- 刪除子模塊目錄及源碼
rm -rf 子模塊目錄
- 刪除.gitmodules中的對應子模塊內容
vi .gitmodules
- 刪除.git/config配置中的對應子模塊內容
vi .git/config
- 刪除.git/modules/下對應子模塊目錄
rm -rf .git/modules/子模塊目錄
- 刪除git索引中的對應子模塊
git rm --cached 子模塊目錄