git submodule使用
文章目录
git submodule 帮助
git submodule [--quiet] [--cached]
git submodule [--quiet] add [<options>] [--] <repository> [<path>]
git submodule [--quiet] status [--cached] [--recursive] [--] [<path>...]
git submodule [--quiet] init [--] [<path>...]
git submodule [--quiet] deinit [-f|--force] (--all|[--] <path>...)
git submodule [--quiet] update [<options>] [--] [<path>...]
git submodule [--quiet] set-branch [<options>] [--] <path>
git submodule [--quiet] set-url [--] <path> <newurl>
git submodule [--quiet] summary [<options>] [--] [<path>...]
git submodule [--quiet] foreach [--recursive] <command>
git submodule [--quiet] sync [--recursive] [--] [<path>...]
git submodule [--quiet] absorbgitdirs [--] [<path>...]
git submodule add <仓库地址> <路径>
自己的项目需要增加git子仓库,使用add命令添加到项目中
git submodule add <仓库地址> <路径>
在指定的路径下安装子仓库,没有路径,
$ git init
Initialized empty Git repository in /mnt/g/workspace/temp/gitSubTest/.git/
$ git submodule add https://gitee.com/scitechlabs/stl_utils.git ./modules/stl_utils
Cloning into '/mnt/g/workspace/temp/testgit/modules/stl_utils'...
remote: Enumerating objects: 55, done.
remote: Counting objects: 100% (55/55), done.
remote: Compressing objects: 100% (55/55), done.
remote: Total 55 (delta 17), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (55/55), 21.44 KiB | 46.00 KiB/s, done.
$ ls modules/stl_utils
README.md src ==============> 在指定的目录出现中仓库的代码
$ cat .gitmodules
[submodule "modules/stl_utils"]
path = modules/stl_utils ==============> 主仓库目录下生成.gitmodules分别记录路径和仓库地址
url = https://gitee.com/scitechlabs/stl_utils.git
$ cat .git/config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
ignorecase = true
[submodule "modules/stl_utils"] ==============> 主仓库的config文件中出现子仓库的信息
url = https://gitee.com/scitechlabs/stl_utils.git
active = true
$ cat ./modules/stl_utils/.git
gitdir: ../../.git/modules/modules/stl_utils ==============> 子仓库的.git变为文件,记录子仓库的.git地址仓库的config文件中出现子仓库的信息
$ cat .git/modules/modules/stl_utils/config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
ignorecase = true
worktree = ../../../../modules/stl_utils ==============> 子仓库的的config中记录worktree
[remote "origin"]
url = https://gitee.com/scitechlabs/stl_utils.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
git submodule deinit -f
反初始化submodule,可以删除主仓库的config中子模块信息,-f 同时删除子仓库数据
$ git submodule deinit stl_utils
error: the following file has changes staged in the index:
stl_utils
(use --cached to keep the file, or -f to force removal)
fatal: Submodule work tree 'stl_utils' contains local modifications; use '-f' to discard them
$ git submodule deinit -f stl_utils
Cleared directory 'stl_utils'
Submodule 'stl_utils' (https://gitee.com/scitechlabs/stl_utils.git) unregistered for path 'stl_utils'
$ ls
stl_utils
$ cat ./.git/config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
ignorecase = true
$ cat .gitmodules
[submodule "modules/stl_utils"]
path = modules/stl_utils
url = https://gitee.com/scitechlabs/stl_utils.git
$ ls ./.git/modules
stl_utils
$ cat ./.git/modules/stl_utils/config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
ignorecase = true
[remote "origin"]
url = https://gitee.com/scitechlabs/stl_utils.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
.gitmodules 中数据需要手工删除
.git/modules 中数据需要手工删除
modules目录需要手工删除
git submodule update --init
如果clone别人的仓库使用子仓库,使用update指定安装仓库
git submodule update --init
切换子仓库分支,提交子仓库代码等操作可以在子仓库目录中直接操作。
git submodule status
git submodule foreach
git submodule foreach git pull
所有子仓库拉取最新版本
git submodule status
$ git submodule status --recursive
15009bd06aecbcef3fcc0de8e631a4c110791a68 doc (heads/master)
92ae6e596c6d0e8c651fbd43c3c592019d421096 src/common (heads/arm)
修改子模块的远程路径
确实,删除子模块然后重新添加是一个更容易理解的方法。以下是具体步骤:
-
删除子模块:
git submodule deinit -f -- path/to/submodule git rm -f path/to/submodule rm -rf .git/modules/path/to/submodule
-
重新添加子模块:
git submodule add [ -b <branch> ] <新的子模块URL> path/to/submodule git submodule update --init --recursive
-
提交更改:
git add .gitmodules git add path/to/submodule git commit -m "Re-add submodule with new URL" git push
通过上述步骤,你可以简单地删除并重新添加子模块,从而更新子模块的URL。
微信号:yjkhtddx