git submodule使用(添加子仓库,删除子仓库,更新子仓库)

git submodule使用

文章目录

  • git submodule使用
    • git submodule 帮助
    • git submodule add
    • git submodule deinit
    • git submodule update

git submodule 帮助

root@A9300:/opt/data/RtspToPsRtp# git submodule --hlep
usage: git submodule [--quiet] add [-b ] [-f|--force] [--name ] [--reference ] [--]  []
   or: git submodule [--quiet] status [--cached] [--recursive] [--] [...]
   or: git submodule [--quiet] init [--] [...]
   or: git submodule [--quiet] deinit [-f|--force] [--] ...
   or: git submodule [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--reference ] [--recursive] [--] [...]
   or: git submodule [--quiet] summary [--cached|--files] [--summary-limit ] [commit] [--] [...]
   or: git submodule [--quiet] foreach [--recursive] 
   or: git submodule [--quiet] sync [--recursive] [--] [...]

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

反初始化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

如果clone别人的仓库使用子仓库,使用update指定安装仓库

git submodule update --init

切换子仓库分支,提交子仓库代码等操作可以在子仓库目录中直接操作。

微信号:yjkhtddx

你可能感兴趣的:(二把刀运维)