在项目开发中,特别是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文件包含以下内容:

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

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

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

(newcnweibo_branch)*$ git status
On branch newcnweibo_branch
Your branch is up-to-date with 'origin/newcnweibo_branch'. Changes to be committed:
(use "git reset HEAD <file>..." to unstage) new file: ../.gitmodules
new file: newtestrepo

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

(newcnweibo_branch)*$  git commit -m "intro newtestrepo submodule"
[newcnweibo_branch 2bb87a3] intro newtestrepo submodule
files changed, insertions(+)
create mode .gitmodules
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

(detached*)$ cat .git
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本身目录都为空!

*$ pwd
/home/cabox/workspace/testgit
*$ git clone https://github.com/cnweibo/githubtest.git
Cloning into 'githubtest'...
remote: Counting objects: , done.
remote: Total (delta ), reused (delta ), pack-reused
Unpacking objects: % (/), done.
Checking connectivity... done.
*$ ls
githubtest
*$ cd githubtest/
(newcnweibo_branch)$ ls
localconfig newweibobranchfile readmegithub submods
(newcnweibo_branch)$ git status
On branch newcnweibo_branch
Your branch is up-to-date with 'origin/newcnweibo_branch'. nothing to commit, working directory clean
(newcnweibo_branch)$ cd submods/
(newcnweibo_branch)$ ls
newtestrepo
(newcnweibo_branch)$ cd newtestrepo/
(newcnweibo_branch)$ ls
(newcnweibo_branch)$ ls -la
total
drwxrwxr-x cabox cabox May : .
drwxrwxr-x cabox cabox May : ..
(newcnweibo_branch)$

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

(newcnweibo_branch)$ pwd
/home/cabox/workspace/testgit/githubtest
(newcnweibo_branch)$ ls -la
total
drwxrwxr-x cabox cabox May : .
drwxrwxr-x cabox cabox May : ..
drwxrwxr-x cabox cabox May : .git
-rw-rw-r-- cabox cabox May : .gitmodules
-rw-rw-r-- cabox cabox May : localconfig
-rw-rw-r-- cabox cabox May : newweibobranchfile
-rw-rw-r-- cabox cabox May : readmegithub
drwxrwxr-x cabox cabox May : submods
(newcnweibo_branch)$ cat .gitmodules
[submodule "submods/newtestrepo"]
path = submods/newtestrepo
url = git@github.com:cnweibo/newtestrepo.git

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

[submodule "submods/newtestrepo"] 这个section,为每一个submodule都正在prepo的.git/config文件中增加一条
[submodule "submods/newtestrepo"]
url = git@github.com:cnweibo/newtestrepo.git 信息!
(newcnweibo_branch)$ pwd
/home/cabox/workspace/testgit/githubtest
(newcnweibo_branch)$ git submodule init
Submodule 'submods/newtestrepo' (git@github.com:cnweibo/newtestrepo.git) registered for path 'su
bmods/newtestrepo'
(newcnweibo_branch)$ ls -la
total
drwxrwxr-x cabox cabox May : .
drwxrwxr-x cabox cabox May : ..
drwxrwxr-x cabox cabox May : .git
-rw-rw-r-- cabox cabox May : .gitmodules
-rw-rw-r-- cabox cabox May : localconfig
-rw-rw-r-- cabox cabox May : newweibobranchfile
-rw-rw-r-- cabox cabox May : readmegithub
drwxrwxr-x cabox cabox May : submods
(newcnweibo_branch)$ cat .git/
HEAD config hooks/ info/ objects/ refs/
branches/ description index logs/ packed-refs
(newcnweibo_branch)$ cat .git/config
[core]
repositoryformatversion =
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = https://github.com/cnweibo/githubtest.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "newcnweibo_branch"]
remote = origin
merge = refs/heads/newcnweibo_branch
[submodule "submods/newtestrepo"]
url = git@github.com:cnweibo/newtestrepo.git
(newcnweibo_branch)$

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

(newcnweibo_branch)$ pwd
/home/cabox/workspace/testgit/githubtest
(newcnweibo_branch)$ ls -la submods/newtestrepo/
total
drwxrwxr-x cabox cabox May : .
drwxrwxr-x cabox cabox May : ..
(newcnweibo_branch)$ git submodule update
Cloning into 'submods/newtestrepo'...
Warning: Permanently added 'github.com,192.30.252.129' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository. Please make sure you have the correct access rights
and the repository exists.
Clone of 'git@github.com:cnweibo/newtestrepo.git' into submodule path 'submods/newtestrepo' failed
$ ssh -T git@github.com
Hi kidsit! You've successfully authenticated, but GitHub does not provide shell access.
$ exit
exit
(newcnweibo_branch)*$ pwd
/home/cabox/workspace/testgit/githubtest
(newcnweibo_branch)*$ git submodule update
Cloning into 'submods/newtestrepo'...
Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.
remote: Counting objects: , done.
remote: Compressing objects: % (/), done.
remote: Total (delta ), reused (delta ), pack-reused
Receiving objects: % (/), done.
Checking connectivity... done.
Submodule path 'submods/newtestrepo': checked out '6c495d0229fcd15fd7e0bb3cded878d0692988a5'
(newcnweibo_branch)$ git submodule update^C
(newcnweibo_branch)$ ls -la submods/newtestrepo/
.git README.md changefromprepo
(newcnweibo_branch)$ ls -la submods/newtestrepo/
.git README.md changefromprepo
(newcnweibo_branch)$ ls -la submods/newtestrepo/
total
drwxrwxr-x cabox cabox May : .
drwxrwxr-x cabox cabox May : ..
-rw-rw-r-- cabox cabox May : .git
-rw-rw-r-- cabox cabox May : README.md
-rw-rw-r-- cabox cabox May : changefromprepo
(newcnweibo_branch)$ cat submods/newtestrepo/.git
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. Codeforces Round #248 (Div. 2)C 题

    题目:http://codeforces.com/contest/433/problem/C 没想到做法就各种纠结, 今天做的都快疯掉了, 太弱了, 等题解一出,就各种恍然大悟 不应该不应该 正文: ...

  2. 输入格式--InputFormat和InputSplit

    1)InputFormat的类图: InputFormat 直接子类有三个:DBInputFormat.DelegatingInputFormat和FileInputFormat,分别表示输入文件的来 ...

  3. 微信公众号token的asp.net脚本

    老板让我搞一个微信公众号.好吧.前面都很EZ,直到要使用一个token验证服务器的有效性. 看了下文档,大概意思就是微信的服务器用GET请求访问你的服务器. 其中包含了signature,nonce, ...

  4. 【面试题】Google of Greater China Test for New Grads of 2014总结

    2014年Google中国校园招聘采用在线技术笔试,在Code Jam平台上,14号9点到15号9点开放测试题,帮助大家熟悉环境.这个周末也有够忙的,当时就上去看了一下,把输入文件下了一下,今天才把题 ...

  5. 在线API文档

    http://www.ostools.net/apidocs A Ace akka2.0.2 Android Ant Apache CXF Apache HTTP服务器 ASM字节码操作 AutoCo ...

  6. ActionResult 返回类型

    类名 抽象类 父类 功能 ContentResult     根据内容的类型和编码,数据内容. EmptyResult     空方法. FileResult abstract   写入文件内容,具体 ...

  7. eclipse下python的selenium自动化环境的搭建

    前提:安装python,我用的2.7.8版本,并在环境变量path里设置;E:\Python1.解压setuptools(Python包管理工具),cmd到目录执行python setup.py in ...

  8. linux下如何查看主机的外网ip地址

    在linux下如果我们使用的是nat方式上网.通过ifconfig命令查看到的ip地址往往是内网地址 那么如何查看主机在互联网上使用的公网IP呢?我们可以在命令行下使用curl命令实现这个功能. [r ...

  9. Linux下ps -ef和ps aux的区别及格式详解

    Linux下显示系统进程的命令ps,最常用的有ps -ef 和ps aux.这两个到底有什么区别呢?两者没太大差别,讨论这个问题,要追溯到Unix系统中的两种风格, System V风格和BSD 风格 ...

  10. cocos2d 播放GIF动画类

    cocos2d 播放GIF动画类 以前项目中曾经用到过,后来因为GIF图像的质量较差,被弃用了,把公司名字去掉分享下,根据网上资料改编的cocos2d-iphone版的. // // CCSprite ...