[root@localhost projects]# git --git-dir=/root/repos/libA.git init --bare Initialized empty Git repository in /root/repos/libA.git/ [root@localhost projects]# git --git-dir=/root/repos/libB.git init --bare Initialized empty Git repository in /root/repos/libB.git/ [root@localhost projects]# ls -al /root/repos total 4 drwxr-xr-x. 4 root root 38 Nov 20 04:35 . dr-xr-x---. 13 root root 4096 Nov 20 04:34 .. drwxr-xr-x. 7 root root 119 Nov 20 04:35 libA.git drwxr-xr-x. 7 root root 119 Nov 20 04:35 libB.git
初始化公共库
获取
1 2 3 4 5 6 7 8 9 10
[root@localhost projects]# git clone /root/repos/libA.git Cloning into 'libA'... warning: You appear to have cloned an empty repository. done. [root@localhost projects]# git clone /root/repos/libB.git Cloning into 'libB'... warning: You appear to have cloned an empty repository. done. [root@localhost projects]#
[root@localhost libB]# cd ../ [root@localhost projects]# git clone /root/repos/project1.git Cloning into 'project1'... warning: You appear to have cloned an empty repository. done. [root@localhost projects]# cd project1/
使用 git submodule add 添加子模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
[root@localhost project1]# git submodule add /root/repos/libA.git libs/libA Cloning into '/root/projects/project1/libs/libA'... done. [root@localhost project1]# git submodule add /root/repos/libB.git libs/libB Cloning into '/root/projects/project1/libs/libB'... done. [root@localhost project1]# git status On branch master
No commits yet
Changes to be committed: (use "git rm --cached <file>..." to unstage)
new file: .gitmodules new file: libs/libA new file: libs/libB
[root@localhost project1-b]# git submodule update Cloning into '/root/projects/project1-b/libs/libA'... done. Cloning into '/root/projects/project1-b/libs/libB'... done. Submodule path 'libs/libA': checked out 'ee8edb6e68cf05a4cceeccb3e0344dc149c5a1ea' Submodule path 'libs/libB': checked out 'f70d346153fe4020db82d3a8b990c1898b6c86fb' [root@localhost project1-b]# tree -L 2 libs/ libs/ ├── libA │ └── readme └── libB └── readme
2 directories, 2 files
修改子模块
查看当前 仓库状态
1 2 3 4 5
[root@localhost project1-b]# git status On branch master Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
如果我们进入子模块A 查看状态呢
1 2 3 4 5 6 7
[root@localhost project1-b]# cd libs/libA/ [root@localhost libA]# git status HEAD detached at ee8edb6 nothing to commit, working tree clean [root@localhost libA]# git branch * (HEAD detached at ee8edb6) master
[root@localhost libA]# git checkout master Switched to branch 'master' Your branch is up to date with 'origin/master'. [root@localhost libA]# echo "add by developer B" >> readme [root@localhost libA]# git commit -am "update libA by developer B" [master 0d13d75] update libA by developer B 1 file changed, 1 insertion(+)
[root@localhost libA]# cd ../../ [root@localhost project1-b]# 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)