Submodule
如果一个git repo中需要嵌套另一个git repo, 可以使用submodule的方式进行嵌入
Add Submodule
git submodule add [-b <branch>] <remote_url> <path/to/repo>在path/to/repo的位置clone一个远程仓库, 并添加为submodule. 所有的submodule保存在git repo根目录的.gitmodules中.
可以通过-b指定分支
Tips
有的时候,
path/to/repo已经在缓存中存在(原来就有一个文件夹). 这个时候应该先从cache中移除这个文件夹:Remove from Staging Area
git rm --cached <file>
- 移除文件在 cache 的记录,但不会删除工作区的文件。
- 常用于让已有的文件被 .gitignore 忽略
git rm <file>Link to original
- 移除 cache 中的记录,同时删除工作区的文件。
Sync URL
注意
这个不会更新repository
在将repo克隆到本地之后, 如果远程的repo将submodule的url改变了(如, 更改成了fork的仓库), 需要更新本地的submodule指向的url.
但是git不允许远程仓库修改本地的配置(.git/config), 只有.gitmodules修改了, 因此需要使用下面的指令进行更新, 以保证Update的正确性:
git submodule syncUpdate
相当于对每一个submodule进行pull操作:
git submodule updateSolve Clone Error
有的时候, 使用克隆的时候忘记使用-r或者--recursive参数, 导致submodule没有拉取. 这个时候可以通过下面的命令重新拉取:
git submodule update --init --recursiveCommit
由于直接clone的仓库的submodule的分支是固定的commit hash, 因此在提交commit的时候需要首先切换到合适的分支(以master为例):
cd path/to/submodule
git checkout <branch:master>
git add <path/to/changes:.>
git commit -m "<message>"
git push
####################
cd path/to/git/root
git add path/to/submodule
git commit -m "update submodule: <submodule>" # or other messages you like
git push但是如果在checkout之前就已经添加了commit, 那么需要用下面的命令进行补错:
cd path/to/submodule
git add .
git commit -m "message"
git checkout master
# supposed that the commit hash is <commit_hash>
git branch <new_branch> <commit_hash>
git merge <commit_hash>
git push
# same
cd path/to/git/root
git add path/to/submodule
git commit -m "<message>"
git push