git 添加子模块
子模块的添加
添加子模块非常简单,命令如下:
git submodule add <url> <path>
其中,url为子模块的路径,path为该子模块存储的目录路径。
执行成功后,git status会看到项目中修改了.gitmodules,并增加了一个新文件(为刚刚添加的路径)
git diff --cached查看修改内容可以看到增加了子模块,并且新文件下为子模块的提交hash摘要
git commit提交即完成子模块的添加


submodule 常用命令
git submodule: 显示所有submodule, 等同于git submodule status- 添加 submodule 到现有项目
- 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
或者直接执行
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, 也可以被执行到. 这个命令比较全面, 会经常使用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 执行命令, 非常有用, 如git submodule foreach 'git checkout main'git diff --submodule: 查看 submodule 所有改变

浙公网安备 33010602011771号