Git Submodule 子模块

Summary: Author: 张亚飞 | 阅读时间: 3 minute read | Published: 2019-10-10
Filed under Categories: LinuxTags: Note,

Git Submodule使用

Git项目子模块可以把项目依赖的第三方库拉取过来,方便项目的更新维护和升级.

以下演示在主项目run_s下添加两个子项目acme-clientqiniu-rs的步骤.

主项目:run_s

~/Server/Run/run_s/

$ git config --list
credential.helper=osxkeychain
http.postbuffer=15728640000
user.name=coam
user.email=zyf@lonal.com
core.eol=lf
core.autocrlf=false
core.ignorecase=false
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=false
core.precomposeunicode=true
core.eol=lf
core.autocrlf=false
remote.origin.url=git@git.dev.tencent.com:coam/Run.run_s.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

添加项目子模块src/libs/acme-client:

~/Server/Run/run_s/

$ git submodule add https://github.com/coam/acme-client.git src/libs/acme-client

添加项目子模块src/libs/qiniu-rs:

~/Server/Run/run_s/

$ git submodule add https://github.com/coam/qiniu-rs.git src/libs/qiniu-rs

最后的子模块配置如下:

~/Server/Run/run_s

[coam@MacPro: ~/Server/Run/run_s]$ git config --list
credential.helper=osxkeychain
http.postbuffer=15728640000
user.name=coam
user.email=zyf@lonal.com
core.eol=lf
core.autocrlf=false
core.ignorecase=false
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=false
core.precomposeunicode=true
core.eol=lf
core.autocrlf=false
remote.origin.url=git@git.dev.tencent.com:coam/Run.run_s.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
submodule.src/libs/qiniu-rs.url=https://github.com/coam/qiniu-rs.git
submodule.src/libs/qiniu-rs.active=true
submodule.src/libs/acme-client.url=https://github.com/coam/acme-client.git
submodule.src/libs/acme-client.active=true

项目下自动添加的子模块配置:

~/Server/Run/run_s/.gitmodules

[submodule "src/libs/qiniu-rs"]
	path = src/libs/qiniu-rs
	url = https://github.com/coam/qiniu-rs.git
[submodule "src/libs/acme-client"]
	path = src/libs/acme-client
	url = https://github.com/coam/acme-client.git

进入项目下的子模块目录

~/Server/Run/run_s/src/libs/acme-client

$ git config --list
credential.helper=osxkeychain
http.postbuffer=15728640000
user.name=coam
user.email=zyf@lonal.com
core.eol=lf
core.autocrlf=false
core.ignorecase=false
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
core.worktree=../../../../../src/libs/acme-client
remote.origin.url=https://github.com/coam/acme-client.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

服务端拉取子模块代码

~/Server/Run/run_s/

$ git pull origin master
Fast-forward
 src/libs/acme-client | 1 +
 src/libs/qiniu-rs    | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
 create mode 160000 src/libs/acme-client

查看git项目子模块配置,发现仅同步下来了子模块文件,并没有配置submodule

~/Server/Run/run_s

$ git config --list
user.email=zyf@lonal.com
user.name=coam
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@git.dev.tencent.com:coam/Run.run_s.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

查看项目依赖所有子模块

~/Server/Run/run_s

$ git config --list
user.email=zyf@lonal.com
user.name=coam
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@git.dev.tencent.com:coam/Run.run_s.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
submodule.src/libs/acme-client.active=true
submodule.src/libs/acme-client.url=https://github.com/coam/acme-client.git
submodule.src/libs/qiniu-rs.active=true
submodule.src/libs/qiniu-rs.url=https://github.com/coam/qiniu-rs.git

子模块代码同步状态

~/Server/Run/run_s

$ git submodule
-04677fdb78227755e43fe817291e9a142bae5b24 src/libs/acme-client
-5026a618754cc5d3b00564553c020d76c328b2d7 src/libs/qiniu-rs

可以看到submodules的状态是hash码和文件目录,但是注意前面有一个减号:-,含义是该子模块代码还没有检出.同步子模块但文件为空同步子模块项目,检出acme-clientqiniu-rssubmodules

~/Server/Run/run_s/

$ git submodule init
Submodule 'src/libs/acme-client' (https://github.com/coam/acme-client.git) registered for path 'src/libs/acme-client'
Submodule 'src/libs/qiniu-rs' (https://github.com/coam/qiniu-rs.git) registered for path 'src/libs/qiniu-rs'
$ git submodule update
Cloning into '/data/home/coam/Server/Run/run_s/src/libs/qiniu-rs'...
Submodule path 'src/libs/acme-client': checked out '04677fdb78227755e43fe817291e9a142bae5b24'
Submodule path 'src/libs/qiniu-rs': checked out '5026a618754cc5d3b00564553c020d76c328b2d7'

再次查看git项目子模块配置

~/Server/Run/run_s

$ git config --list
user.email=zyf@lonal.com
user.name=coam
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@git.dev.tencent.com:coam/Run.run_s.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
submodule.src/libs/acme-client.active=true
submodule.src/libs/acme-client.url=https://github.com/coam/acme-client.git
submodule.src/libs/qiniu-rs.active=true
submodule.src/libs/qiniu-rs.url=https://github.com/coam/qiniu-rs.git

查看项目依赖子模块配置

~/Server/Run/run_s

$ git submodule
 04677fdb78227755e43fe817291e9a142bae5b24 src/libs/acme-client (acme-client-0.5.3-2-g04677fd)
 5026a618754cc5d3b00564553c020d76c328b2d7 src/libs/qiniu-rs (heads/master)

向上同步子模块

当子模块文件有更新或切换分支时,如果要使用需要在主项目提交

~/Server/Run/run_s

git add sub-module
git commit -m "use sub-module version 1.1"

向下同步子模块

当子模块文件有代码更新需要同步时,在主项目更新拉取子模块

~/Server/Run/run_s

git submodule update

删除项目子模块src/libs/qiniu-rs:

~/Server/Run/run_s

rm -rf src/libs/acme-client
git config --unset submodule.src/libs/acme-client.url
git config --unset submodule.src/libs/acme-client.active

Reference


子模块总结

以项目run_s为例

添加子模块

~/Server/Run/run_s

$ git submodule add https://github.com/coam/acme-client.git src/libs/acme-client
$ git submodule add https://github.com/coam/qiniu-rs.git src/libs/qiniu-rs

提交到项目仓库

~/Server/Run/run_s

$ git push origin master

服务器发布及同步子模块

~/Server/Run/run_s

$ git pull origin master
$ git submodule
$ git submodule init
$ git submodule update

Comments

  • LesGync says: 2020-04-22 10:59:28

    Achat Lasilix 100 Mg En France https://buycialisuss.com/# - Buy Cialis Viagra A Poco Prezzo <a href=https://buycialisuss.com/#>Buy Cialis</a> Order Alli

  • says: 2019-10-10 23:43:54

Cor-Ethan, the beverage → www.iirii.com