参考http://www.diguage.com/archives/146.html
一、添加子模块
从新建一个项目,或者从远处服务器上克隆一个项目,作为“顶级项目”。这里,从 Github 上新建一个项目,然后再克隆下来:
➜ git clone git@github.com:diguage/parent.git Cloning into 'parent'... remote: Counting objects: 5, done. remote: Compressing objects: 100% (5/5), done. remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (5/5), 4.59 KiB | 0 bytes/s, done. Checking connectivity... done.
进入刚克隆下来的项目,我们来添加子模块。这里的子模块就必须从远处服务器上克隆了。另外,子模块可以放在项目的任意目录下,并且可以根据自己的需要重命名。为了方便演示,所以就直接放在子目录了。进入顶级项目,我们先看一下版本库的状态:
➜ cd parent ➜ git status #查看版本库的状态
On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working directory clean
这里提示,项目中没有任何需要提交的东西。下面,我们开始添加子模块:
命令如下:git submodule add 仓库地址 路径
➜ git submodule add git@github.com:diguage/child.git #添加子模块child Cloning into 'child'... remote: Counting objects: 5, done. remote: Compressing objects: 100% (5/5), done. remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (5/5), 4.63 KiB | 0 bytes/s, done. Checking connectivity... done.
这时,再看一下版本库的状态:
➜ git status
On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: .gitmodules new file: child
这时,我们看到,除了刚刚添加的 child
模块外,还多了一个 .gitmodules
文件。这个文件就是用于记录子模块的路径已经远程版本库地址的地方。这两个文件处于待提交的状态。下面把这个修改提交一下。
➜ git commit -am "增加子模块" [master a05bfe0] 增加子模块 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 child ➜ git push origin master Counting objects: 3, done. Delta compression using up to 8 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 452 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To git@github.com:diguage/parent.git e56cd5a..a05bfe0 master -> master
这时,大家可以登录到 Github 或者其他的版本库服务器上看一下项目的状态。至此,添加子模块的工作已经完成。
二、更新子模块
当我们添加完子模块后,过段时间后,子模块有更新,这时候,我们就需要更新了。
方式一
➜ cd child ➜ git pull origin master remote: Counting objects: 3, done. remote: Compressing objects: 100% (3/3), done. remote: Total 3 (delta 2), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From github.com:diguage/child * branch master -> FETCH_HEAD d4ec39a..1b855a4 master -> origin/master Updating d4ec39a..1b855a4 Fast-forward README.md | 3 +++ 1 file changed, 3 insertions(+)
再进入到顶级项目中,查看一下项目状态:
➜ cd .. ➜ git status On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: child (new commits) no changes added to commit (use "git add" and/or "git commit -a")
这里可以看到明显的提示,在项目有一个新的提交。为什么这样呢?其实,Git 在顶级项目中记录了一个子模块的提交日志的指针,用于保存子模块的提交日志所处的位置,以保证无论子模块是否有新的提交,在任何一个地方克隆下顶级项目时,各个子模块的记录是一致的。避免因为所引用的子模块不一致导致的潜在问题。如果我们更新了子模块,我们需要把这个最近的记录提交到版本库中,以方便和其他人协同。这也是刚刚添加完子模块后还要在顶级项目中提交一次的原因。
➜ git commit -am "更新子模块" [master 5ac4dbb] 更新子模块 1 file changed, 1 insertion(+), 1 deletion(-) ➜ git push origin master Counting objects: 2, done. Delta compression using up to 8 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (2/2), 259 bytes | 0 bytes/s, done. Total 2 (delta 1), reused 0 (delta 0) To git@github.com:diguage/parent.git a05bfe0..5ac4dbb master -> master
备注:进入子模块目录后,你就当做这是一个独立的项目,可以正常进行一个普通 Git 项目所执行的任何操作。更新完正常的修改、添加任务后,把所做的修改提交,然后推送到远处版本库上。然后,如果需要记得在顶级项目中,也做一次提交,修改一下顶级项目所引用的子模块的提交日志的“指针”。
三、克隆项目[这个我实践过,别的尚未实践]
当多人合作搞一个项目,需要从远处版本库上克隆项目代码。当使用git clone下来的工程中带有submodule时,初始的时候,submodule的内容并不会自动下载下来的,此时,只需执行如下命令:git submodule update --init --recursive
即可将子模块内容下载下来后工程才不会缺少相应的文件。
方式一(比较麻烦,但是是基础的方式)
$ git clone https://github.com/ethz-asl/rovio.git # 第一步
Cloning into 'rovio'...
remote: Counting objects: 2759, done.
remote: Total 2759 (delta 0), reused 0 (delta 0), pack-reused 2759
Receiving objects: 100% (2759/2759), 964.96 KiB | 7.00 KiB/s, done.
Resolving deltas: 100% (1808/1808), done.
Checking connectivity... done. $ cd rovio $ git submodule update --init --recursive # 第二步
Submodule 'lightweight_filtering' (https://bitbucket.org/bloesch/lightweight_filtering.git) registered for path 'lightweight_filtering'
Cloning into 'lightweight_filtering'...
remote: Counting objects: 1541, done.
remote: Compressing objects: 100% (921/921), done.
remote: Total 1541 (delta 691), reused 0 (delta 0)
Receiving objects: 100% (1541/1541), 319.81 KiB | 99.00 KiB/s, done.
Resolving deltas: 100% (1050/1050), done.
Checking connectivity... done.
Submodule path 'lightweight_filtering': checked out 'b1d235dca7646c013c96c9335c3d8a982b4244b5'
$ cd lightweight_filtering $ git status # 第三步
HEAD detached at b1d235d
nothing to commit, working directory clean $ git checkout master # 第四步
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
方式二(比较简单的方式)
$ git clone --recursive https://github.com/ethz-asl/rovio.git # 第一步
$ cd rovio/lightweight_filtering # 第二步
$ git checkout master # 第三步
四、删除子模块
首先,要在“.gitmodules”文件中删除相应配置信息。
然后,执行“git rm 子模块 ”命令将子模块所在的文件从git中删除。
➜ git rm child #删除子模块
rm 'child'
➜ git status #查看版本库的状态
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: .gitmodules
deleted: child
➜ git commit -am "删除子模块" #删除完子模块后还要在顶级项目中提交一次
[master b0b088b] 删除子模块
2 files changed, 4 deletions(-)
delete mode 160000 child
➜ git push origin master
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 274 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@github.com:diguage/parent.git
5ac4dbb..b0b088b master -> master