git 添加子模塊
子模塊的添加
添加子模塊非常簡單,命令如下:
git submodule add <url> <path>
其中,url為子模塊的路徑,path為該子模塊存儲的目錄路徑。
執(zhí)行成功后,git status會看到項目中修改了.gitmodules,并增加了一個新文件(為剛剛添加的路徑)
git diff --cached查看修改內(nèi)容可以看到增加了子模塊,并且新文件下為子模塊的提交hash摘要
git commit提交即完成子模塊的添加


submodule 常用命令
git submodule: 顯示所有submodule, 等同于git submodule status- 添加 submodule 到現(xiàn)有項目
- Run
git submodule add -b <branch> --name <name> <repo-url> <local dir> - Commit both files on the superproject
- Run
- 從當前項目移除 submodule
git submodule deinit -f <submodule_path>rm -rf .git/modules/<submodule_path>git rm -f <submodule_path>
-
復制含 submodule 項目到本地
- Clone the superproject as usual
- Run
git submodule initto init the submodules - Run
git submodule updateto have the submodules on a detached HEAD
或者直接執(zhí)行
git clone --recurse-submodules <repo-url> git submodule init: 將本項目所依賴的submodule進行初始化git submodule update: 更新 submodule 為superproject本次 commit 所記錄的版本 (本地版本為舊版本的話那么就與舊版本保持同步!)git submodule update --init: 前面兩個命令的合并git submodule update --init --recursive: 前面三個命令的合集,--recursive是為了保證即使submodule又嵌套了sub-submodule, 也可以被執(zhí)行到. 這個命令比較全面, 會經(jīng)常使用git submodule update --remote: 更新 submodule 為遠程項目的最新版本 (更為常用!)git submodule update --remote <submodule-name>: 更新指定的 submodule 為遠程的最新版本git push --recurse-submodules=check: 檢查submodule是否有提交未推送, 如果有, 則使本次提交失敗on-demand: 先推送 submodule 的更新, 然后推送主項目的更新 (如果 submodule 推送失敗, 那么推送任務直接終止)while: 所有的submodule會被依次推送到遠端, 但是superproject將不會被推送no: 與while相反, 只推送superproject, 不推送其他submodule
git pull --recurse-submodules: 拉取所有子倉庫 (fetch) 并 merge 到所跟蹤的分支上git diff --submodule: 查看 submodule 所有改變git submodule foreach '<arbitrary-command-to-run>': 對所有 submodule 執(zhí)行命令, 非常有用, 如git submodule foreach 'git checkout main'git diff --submodule: 查看 submodule 所有改變

浙公網(wǎng)安備 33010602011771號