tips for git submodule

作者 YYGCui 日期 2015-10-25
Categories: 技术积累 Tags: git submodule
tips for git submodule

近来使用git submodule来管理项目,趟过一些坑,整理记录一下。



A submodule allows you to keep another Git repository in a subdirectory of your repository. The other repository has its own history, which does not interfere with the history of the current repository. This can be used to have external dependencies such as third party libraries for example.

##Add submodule


git submodule [--quiet] add [-b <branch>] [-f|--force] [--name <name>]
[--reference <repository>] [--depth <depth>] [--] <repository> [<path>]


[submodule "submodule-name"]
path = path name
url =

##update submodule


git submodule [--quiet] init [--] [<path>…​]
git submodule [--quiet] update [--init] [--remote] [-N|--no-fetch]
[-f|--force] [--rebase|--merge] [--reference <repository>]
[--depth <depth>] [--recursive] [--] [<path>…​]


如果需要从远程repo中update submodule,需要在update时加入--remote参数,这样会pull下来server上的最新版。

##modify submodule repo


git submodule [--quiet] sync [--recursive] [--] [<path>…​]

##remove submodule


mv asubmodule asubmodule_tmp
git submodule [--quiet] deinit [-f|--force] [--] <path>…​
git rm asubmodule
# if you want to leave it in your working tree
git rm --cached asubmodule
mv asubmodule_tmp asubmodule
rm -rf .git/modules/asubmodule

##modify submodule content

update之后的submodule是不属于任何一个branch的,也就是无branch的。submodule的HEAD是处于detached HEAD状态的。这样,如果要更改submodule的内容,需要首先切换到要更新的branch,然后按正常的git流程add/commit/pull/push等操作。


yygcui@ubuntu$ git checkout master
Warning: you are leaving 1 commit behind, not connected to
any of your branches:
<commit id> forget to check out master
If you want to keep them by creating a new branch, this may be a good time
to do so with:
git branch new_branch_name <commit id>
Switched to branch 'master'
yygcui@ubuntu$ git cherry-pick <commit id>
yygcui@ubuntu$ git push