如果你的项目中有很多第三方的lib,你希望使用它,并且也希望可能对该lib做修改并且贡献到原始的项目中去,或者你的项目希望模块化,分为几个repo单独维护,那么git subtree就是一个选择。
git subtree是附属于git的附加工具,默认情况下并不会安装。https://github.com/git/git/tree/master/contrib/subtree。
#
git clone https://github.com/git# cd git# make prefix=/usr/local/git install
# echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/bashrc # source /etc/bashrc# cd contrib/subtree# make
prefix=/usr/local/git/bin
# make
prefix=/usr/local/git/bin install
# make
prefix=/usr/local/git/bin install-doc
# cp git-subtree
/usr/local/git/bin
就ok了!
在本文中,我们假设我们工作在subtree-p这个项目中(放在github上的一个repo),而在该项目中,我们需要subtree-sub这个子模块(或者说是Lib).
当前我们的subtree-p项目中,没有引入任何subtree-sub这个模块,现在我们通过以下命令来实现:
(master)$ git remote add subtree-sub-remote https://github.com/cnweibo/subtree-sub.git (master)$ git remote -v origin https://github.com/cnweibo/subtree-p.git (fetch) origin https://github.com/cnweibo/subtree-p.git (push) subtree-sub-remote https://github.com/cnweibo/subtree-sub.git (fetch) subtree-sub-remote https://github.com/cnweibo/subtree-sub.git (push) (master)$ pwd /home/cabox/workspace/subtreetest/subtree-p (master)$ ls README.md (master)$ git subtree add --prefix=mysubtree subtree-sub-remote master git fetch subtree-sub-remote master warning: no common commits remote: Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/cnweibo/subtree-sub * branch master -> FETCH_HEAD * [new branch] master -> subtree-sub-remote/master Added dir ‘mysubtree‘ (master)$ ls README.md mysubtree (master)$ ls -la mysubtree/ total 12 drwxrwxr-x 2 cabox cabox 4096 May 31 02:14 . drwxrwxr-x 4 cabox cabox 4096 May 31 02:14 .. -rw-rw-r-- 1 cabox cabox 14 May 31 02:14 README.md
上面的一系列命令中,首先在subtree-p项目目录中引入了subtree-sub这个子项目,它存放在目录mysubtree中,和我们的subtree-sub-remote这个remote相对应。这时,subtree-p项目由于增加了新的目录(subtree-sub子项目repo),因此需要commit和push.
注意:在mysubtree这个子目录中是没有任何.git文件的,它实际上是subtree-sub这个子项目repo的纯代码copy!!
(master)$ git s On branch master Your branch is ahead of ‘origin/master‘ by 2 commits. (use "git push" to publish your local commits) nothing to commit, working directory clean (master)$ git push Username for ‘https://github.com‘: kidsit Password for ‘https://[email protected]‘: Counting objects: 5, done. Delta compression using up to 12 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (5/5), 528 bytes | 0 bytes/s, done. Total 5 (delta 0), reused 0 (delta 0) To https://github.com/cnweibo/subtree-p.git 67da25d..c1e6018 master -> master
这时,我们查看在github上的subtree-p repo,你就可以看到这个父项目已经引入了mysubtree(代表了subtree-sub repo!)
现在我们在parent项目中对子项目做修改并且commit, push:
(master)$ echo "kidsit add subfile1 within parent" >subfile1 (master)*$ git s On branch master Your branch is up-to-date with ‘origin/master‘. Untracked files: (use "git add <file>..." to include in what will be committed) subfile1 nothing added to commit but untracked files present (use "git add" to track) (master)*$ git a . (master)*$ git ci "kidsit add subfile1 within parent" [master ed30a68] kidsit add subfile1 within parent 1 file changed, 1 insertion(+) create mode 100644 mysubtree/subfile1 (master)$ git s On branch master Your branch is ahead of ‘origin/master‘ by 1 commit. (use "git push" to publish your local commits) nothing to commit, working directory clean (master)$ git push Username for ‘https://github.com‘: kidsit Password for ‘https://[email protected]‘: Counting objects: 4, done. Delta compression using up to 12 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (4/4), 398 bytes | 0 bytes/s, done. Total 4 (delta 0), reused 0 (delta 0) To https://github.com/cnweibo/subtree-p.git c1e6018..ed30a68 master -> master (master)$
这时我们再查看github上的subtree-p我们就能看到我们对子项目的新的改动了:
我们来继续模拟代码开发过程:修改subtree-p和subtree-sub后,统一一次性做一个提交:
(master)$ echo "kidsit change README for p" >README.md (master)*$ echo "kidsit add new subfileNew2 " >mysubtree/subfileNew2 (master)*$ git s 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: README.md Untracked files: (use "git add <file>..." to include in what will be committed) mysubtree/subfileNew2 no changes added to commit (use "git add" and/or "git commit -a") (master)*$ git a . (master)*$ git ci "kidsit change p and s in ONE commit" [master a27d335] kidsit change p and s in ONE commit 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 mysubtree/subfileNew2 (master)$ git push Username for ‘https://github.com‘: kidsit Password for ‘https://[email protected]‘: Counting objects: 5, done. Delta compression using up to 12 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (5/5), 461 bytes | 0 bytes/s, done. Total 5 (delta 0), reused 0 (delta 0) To https://github.com/cnweibo/subtree-p.git ed30a68..a27d335 master -> master (master)$
这时,我们要注意,我们再subtree-p项目中对父项目和子项目分别都做了修改,但是子项目本身的repo还没有做过任何更新,它根本不知道你曾经做过修改,这时,如果你希望将你对子项目的修改贡献出来,可以执行git subtree push命令
(master)$ git push Username for ‘https://github.com‘: kidsit Password for ‘https://[email protected]‘: Counting objects: 5, done. Delta compression using up to 12 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (5/5), 461 bytes | 0 bytes/s, done. Total 5 (delta 0), reused 0 (delta 0) To https://github.com/cnweibo/subtree-p.git ed30a68..a27d335 master -> master (master)$ git subtree push --prefix=mysubtree/ subtree-sub-remote master git push using: subtree-sub-remote master Username for ‘https://github.com‘: kidsit Password for ‘https://[email protected]‘: Counting objects: 6, done. Delta compression using up to 12 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (6/6), 563 bytes | 0 bytes/s, done. Total 6 (delta 1), reused 0 (delta 0) To https://github.com/cnweibo/subtree-sub.git 9a0ef93..001c5d8 001c5d86c4da43ab676aaeb49763d5353353159f -> master
上面我们分别对subtree-p这个父项目和subtree-sub这个子项目分别作了push操作分享(一般来说对子项目push是非常少见的动作,因为我们更多的是拿别人的lib来用而不是开发),这时如果我们去看github上的两个项目repo的commit历史,你会发现两个repo上都有了同一个commit!!
正因为如此,我们最好是将父项目和子项目分别做commit,不要混淆在一起!这样做的好处是git subtree push时,git会自动将对父项目和子项目的commit区分清楚的,对父项目的修改不会出现在子项目repo修改历史中!