在项目开发中,特别是web前端开发中,有非常多的开源第三方library,我们希望引用他们,同时也希望能够方便地保持这些第三方

开源repo的更新。另外一方面如果我们自己在开发一个网站的项目,这个项目一般分为前端和后端两个相对独立的子项目,特别是前端的repo可能在不同的项目中共享,那么这时,你就可能希望将项目分开为前端和后端两个repo,如何管理这种情况呢?一个比较好的方案就是使用git的submodule功能。

  假设我们的父repo在prepo目录,sumodule newtestrepo希望放在prepo/submods/newtestrepo这个目录,首先我们cd submods目录,

  1. 在submods目录下执行:git submodule add https://github.com/cnweibo/newtestrepo.git  这个命令将在prepo目录下创建.gitmodules文件以及在prepo/submods/目录下创建newtestrepo目录用于保存newtestrepo内容。.gitmodules文件包含以下内容:

  1. [submodule "submods/newtestrepo"]
  2. path = submods/newtestrepo
  3. url = https://github.com/cnweibo/newtestrepo.git

注意:在1.8版本git之前,上述命令必须在preop的root目录下执行!

2.执行git status,则发现有两个变更需要commit

  1. (newcnweibo_branch)*$ git status
  2. On branch newcnweibo_branch
  3. Your branch is up-to-date with 'origin/newcnweibo_branch'.
  4.  
  5. Changes to be committed:
  6. (use "git reset HEAD <file>..." to unstage)
  7.  
  8. new file: ../.gitmodules
  9. new file: newtestrepo

3.执行git commit将增加submodule newtestrepo这个commit做一下提交

  1. (newcnweibo_branch)*$ git commit -m "intro newtestrepo submodule"
  2. [newcnweibo_branch 2bb87a3] intro newtestrepo submodule
  3. files changed, insertions(+)
  4. create mode .gitmodules
  5. create mode submods/newtestrepo

4. git push 将上述修改放到preop的中央库中去以便其他teammember使用

5. 如何在prepo这个项目中修改submodule呢?git checkout master 进入newtestrepo的master branch, 修改文件,commit,随后git push,注意这里的push是将submodule push到中央库中。注意:submodule/newtestrepo目录下不再有.git目录,而只有一个.git文件,这一点很是奇妙!

在我们的submodule的目录中,你查看一下.git这个文件(不是文件夹哦!)的内容实际上指示列出来一个引用gitdir指向prepo的.git/modules/xxxdir/xxxrepo

  1. (detached*)$ cat .git
  2. gitdir: ../../.git/modules/submods/newtestrepo

6. 注意,这时如果我们到prepo的目录中,git status发现我们又有了两个没有commit的commit。

随后我们需要将prepo也做push以便将上述两个commits递交.

之所以在submodule中修改并且push后还要在prepo中push是因为我们的父repo其实是引用了子module的一个snapshot,子module修改后,父repo并没有修改对版本的引用,因此需要commit来反映这个变化。

7.当我们另外一个teammember clone这个prepo时,注意,prepo里面所包含的submodule本身目录都为空!

  1. *$ pwd
  2. /home/cabox/workspace/testgit
  3. *$ git clone https://github.com/cnweibo/githubtest.git
  4. Cloning into 'githubtest'...
  5. remote: Counting objects: , done.
  6. remote: Total (delta ), reused (delta ), pack-reused
  7. Unpacking objects: % (/), done.
  8. Checking connectivity... done.
  9. *$ ls
  10. githubtest
  11. *$ cd githubtest/
  12. (newcnweibo_branch)$ ls
  13. localconfig newweibobranchfile readmegithub submods
  14. (newcnweibo_branch)$ git status
  15. On branch newcnweibo_branch
  16. Your branch is up-to-date with 'origin/newcnweibo_branch'.
  17.  
  18. nothing to commit, working directory clean
  19. (newcnweibo_branch)$ cd submods/
  20. (newcnweibo_branch)$ ls
  21. newtestrepo
  22. (newcnweibo_branch)$ cd newtestrepo/
  23. (newcnweibo_branch)$ ls
  24. (newcnweibo_branch)$ ls -la
  25. total
  26. drwxrwxr-x cabox cabox May : .
  27. drwxrwxr-x cabox cabox May : ..
  28. (newcnweibo_branch)$

但是我们的prepo: githubtest本身却有一个文件.gitmodules,它列出了在prepo中是如何引用sub module的

  1. (newcnweibo_branch)$ pwd
  2. /home/cabox/workspace/testgit/githubtest
  3. (newcnweibo_branch)$ ls -la
  4. total
  5. drwxrwxr-x cabox cabox May : .
  6. drwxrwxr-x cabox cabox May : ..
  7. drwxrwxr-x cabox cabox May : .git
  8. -rw-rw-r-- cabox cabox May : .gitmodules
  9. -rw-rw-r-- cabox cabox May : localconfig
  10. -rw-rw-r-- cabox cabox May : newweibobranchfile
  11. -rw-rw-r-- cabox cabox May : readmegithub
  12. drwxrwxr-x cabox cabox May : submods
  13. (newcnweibo_branch)$ cat .gitmodules
  14. [submodule "submods/newtestrepo"]
  15. path = submods/newtestrepo
  16. url = git@github.com:cnweibo/newtestrepo.git

8.为了能够正常使用submodule,我们必须在新clone的prepo中执行 git submodule init命令,执行该命令时:git将通过检查.gitmodules文件中的

  1. [submodule "submods/newtestrepo"] 这个section,为每一个submodule都正在prepo的.git/config文件中增加一条
  1. [submodule "submods/newtestrepo"]
  2. url = git@github.com:cnweibo/newtestrepo.git 信息!
  1. (newcnweibo_branch)$ pwd
  2. /home/cabox/workspace/testgit/githubtest
  3. (newcnweibo_branch)$ git submodule init
  4. Submodule 'submods/newtestrepo' (git@github.com:cnweibo/newtestrepo.git) registered for path 'su
  5. bmods/newtestrepo'
  6. (newcnweibo_branch)$ ls -la
  7. total
  8. drwxrwxr-x cabox cabox May : .
  9. drwxrwxr-x cabox cabox May : ..
  10. drwxrwxr-x cabox cabox May : .git
  11. -rw-rw-r-- cabox cabox May : .gitmodules
  12. -rw-rw-r-- cabox cabox May : localconfig
  13. -rw-rw-r-- cabox cabox May : newweibobranchfile
  14. -rw-rw-r-- cabox cabox May : readmegithub
  15. drwxrwxr-x cabox cabox May : submods
  16. (newcnweibo_branch)$ cat .git/
  17. HEAD config hooks/ info/ objects/ refs/
  18. branches/ description index logs/ packed-refs
  19. (newcnweibo_branch)$ cat .git/config
  20. [core]
  21. repositoryformatversion =
  22. filemode = true
  23. bare = false
  24. logallrefupdates = true
  25. [remote "origin"]
  26. url = https://github.com/cnweibo/githubtest.git
  27. fetch = +refs/heads/*:refs/remotes/origin/*
  28. [branch "newcnweibo_branch"]
  29. remote = origin
  30. merge = refs/heads/newcnweibo_branch
  31. [submodule "submods/newtestrepo"]
  32. url = git@github.com:cnweibo/newtestrepo.git
  33. (newcnweibo_branch)$

9.随后我们在prepo再执行git submodule update,这时git将通过Internet把所有的submodule clone进入我们的子目录中(由.gitmodules文件来决定放到哪个子目录中)

  1. (newcnweibo_branch)$ pwd
  2. /home/cabox/workspace/testgit/githubtest
  3. (newcnweibo_branch)$ ls -la submods/newtestrepo/
  4. total
  5. drwxrwxr-x cabox cabox May : .
  6. drwxrwxr-x cabox cabox May : ..
  7. (newcnweibo_branch)$ git submodule update
  8. Cloning into 'submods/newtestrepo'...
  9. Warning: Permanently added 'github.com,192.30.252.129' (RSA) to the list of known hosts.
  10. Permission denied (publickey).
  11. fatal: Could not read from remote repository.
  12.  
  13. Please make sure you have the correct access rights
  14. and the repository exists.
  15. Clone of 'git@github.com:cnweibo/newtestrepo.git' into submodule path 'submods/newtestrepo' failed
  16. $ ssh -T git@github.com
  17. Hi kidsit! You've successfully authenticated, but GitHub does not provide shell access.
  18. $ exit
  19. exit
  20. (newcnweibo_branch)*$ pwd
  21. /home/cabox/workspace/testgit/githubtest
  22. (newcnweibo_branch)*$ git submodule update
  23. Cloning into 'submods/newtestrepo'...
  24. Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.
  25. remote: Counting objects: , done.
  26. remote: Compressing objects: % (/), done.
  27. remote: Total (delta ), reused (delta ), pack-reused
  28. Receiving objects: % (/), done.
  29. Checking connectivity... done.
  30. Submodule path 'submods/newtestrepo': checked out '6c495d0229fcd15fd7e0bb3cded878d0692988a5'
  31. (newcnweibo_branch)$ git submodule update^C
  32. (newcnweibo_branch)$ ls -la submods/newtestrepo/
  33. .git README.md changefromprepo
  34. (newcnweibo_branch)$ ls -la submods/newtestrepo/
  35. .git README.md changefromprepo
  36. (newcnweibo_branch)$ ls -la submods/newtestrepo/
  37. total
  38. drwxrwxr-x cabox cabox May : .
  39. drwxrwxr-x cabox cabox May : ..
  40. -rw-rw-r-- cabox cabox May : .git
  41. -rw-rw-r-- cabox cabox May : README.md
  42. -rw-rw-r-- cabox cabox May : changefromprepo
  43. (newcnweibo_branch)$ cat submods/newtestrepo/.git
  44. gitdir: ../../.git/modules/submods/newtestrepo

使用git submodule管理一个需要多个分立开发或者第三方repo的项目的更多相关文章

  1. [转]使用Git Submodule管理子模块

    本文转自:https://blog.csdn.net/qq_37788558/article/details/78668345 实例代码: 父项目:https://github.com/jjz/pod ...

  2. 使用Git Submodule管理子模块

    转自:https://segmentfault.com/a/1190000003076028 使用场景 基于公司的项目会越来越多,常常需要提取一个公共的类库提供给多个项目使用,但是这个library怎 ...

  3. git submodule 管理子项目

    使用场景 拆分项目,当项目越来越大之后,我们希望 子模块 可以单独管理,并由 专门 的人去维护,这个时候只可以使用 git submodule 去完成. 常用命令 git clone <repo ...

  4. 转:Git Submodule管理项目子模块

    使用场景 当项目越来越庞大之后,不可避免的要拆分成多个子模块,我们希望各个子模块有独立的版本管理,并且由专门的人去维护,这时候我们就要用到git的submodule功能. 常用命令 git clone ...

  5. Git Submodule管理项目子模块

    使用场景 当项目越来越庞大之后,不可避免的要拆分成多个子模块,我们希望各个子模块有独立的版本管理,并且由专门的人去维护,这时候我们就要用到git的submodule功能. 常用命令 git clone ...

  6. git submodule的使用

    1.在项目中使用Submodule 为当前工程添加submodule,命令如下:git submodule add 仓库地址 路径仓库地址:是指子模块仓库地址URL.路径:指将子模块放置在当前工程下的 ...

  7. Git 2.7: 一个新的带来许多新特性和性能提升的主要版本

    在2.6版本发布两个月之后,Git 2.7发布.该版本带来了许多新特性以及性能的提升. 本文选取了Git 2.7带来的主要变化: git remote支持get-url子命令,可以显示指定远端的URL ...

  8. git submodule 使用小结

    git submodule 使用小结 原文链接 http://blog.gezhiqiang.com/2017/03/08/git-submodule/###### Git Submodule 允许一 ...

  9. Git 分支管理策略汇总

    原文链接: Git 分支管理策略 最近,团队新入职了一些小伙伴,在开发过程中,他们问我 Git 分支是如何管理的,以及应该怎么提交代码? 我大概说了一些规则,但仔细想来,好像也并没有形成一个清晰规范的 ...

随机推荐

  1. 使用Assetbundle时可能遇到的坑

    原地址:http://www.cnblogs.com/realtimepixels/p/3652128.html 一 24 十一郎未分类 No Comments 转自 http://www.unity ...

  2. 使用tomcat7创建异步servlet

    该篇文章翻译自:http://developerlife.com/tutorials/?p=1437 一.简介 Servlet API 3.0 之前,需要使用类似Comet的方式来实现创建异步的Ser ...

  3. 调用MYSQL存储过程实例

    PHP调用MYSQL存储过程实例 http://blog.csdn.net/ewing333/article/details/5906887 http://www.cnblogs.com/kkchen ...

  4. iOS第三方解决键盘遮挡-IQKeyboardManager

    百度云:http://pan.baidu.com/s/1yg5ae githun:https://github.com/hackiftekhar/IQKeyboardManager AppDelega ...

  5. POJ 2070

    #include<iostream> #include<stdio.h> using namespace std; int main() { //freopen("a ...

  6. 屏蔽wordpress升级提示

    根据自己的需要,挑选适合的代码放在主题的functions.php文件中就可以了 /* 去除 WordPress 後台升級提示 */ // 2.8 ~ 2.9: add_filter('pre_tra ...

  7. Java常用类库

    System System:类中的方法和属性都是静态的. out:标准输出,默认是控制台. in:标准输入,默认是键盘. System描述系统一些信息.获取系统属性信息:Properties getP ...

  8. Jmeter之Bean shell使用(二)

    上一篇Jmeter之Bean shell使用(一)简单介绍了下Jmeter中的Bean shell,本文是对上文的一个补充,主要总结下常用的几种场景和方法,相信这些基本可以涵盖大部分的需求.本节内容如 ...

  9. Junit4中的新断言assertThat的使用方法

    如果需要是用assertThat需要在项目中引入junit4的jar包.(匹配器和断言方法在junit4的jar包中都能找到,引入就可以了) 下面是常用断言的代码 1 import static or ...

  10. lintcode:将二叉查找树转换成双链表

    题目 将一个二叉查找树按照中序遍历转换成双向链表 给定一个二叉查找树: 4 / \ 2 5 / \ 1 3 返回 1<->2<->3<->4<->5. ...