Git Submodule 常用命令

Git Submodule是Git版本控制系统中一个重要的功能,它可以帮助开发人员更好地管理项目的依赖和子模块。 本文将简介Git Submodule和一些常用命令。

Git Submodule

有种情况我们经常会遇到:某个工作中的项目需要包含并使用另一个项目。 也许是第三方库,或者你独立开发的,用于多个父项目的库。 现在问题来了:你想要把它们当做两个独立的项目,同时又想在一个项目中使用另一个。

Git 通过子模块来解决这个问题。 子模块允许你将一个 Git 仓库作为另一个 Git 仓库的子目录。 它能让你将另一个仓库克隆到自己的项目中,同时还保持提交的独立。

常用命令

1. 添加、初始化

可以以下命令,指定子模块的仓库地址和存放目录:

js 复制代码
git submodule add

进入主Git的工作目录 ./awe/,执行add

js 复制代码
cd ./awe/ 
 git submodule add https://gitee.com/lucktk/go-development-practices.git xss\practices

其中 gitee.com/lucktk/go-d... 替换为子模块的Git地址,xss\practices 替换为子模块存放的目录,如此,会在根目录下生成.gitmodules的文件,内容如下:

js 复制代码
[submodule "xss/practices"]
	path = xss/practices
	url = https://gitee.com/lucktk/go-development-practices.git

保存了子模块的Git信息和放置的目录。

然后,可以以下命令初始化子模块

js 复制代码
git submodule init

2. 查询版本

可以以下命令,查询子模块对应的版本号(commit id)

js 复制代码
git submodule

例如:

js 复制代码
git submodule
 2194afff849470b11dedbaed6bfb2fdbcc20352d xss/practices (heads/master)

3. 更新

可以使用以下命令,更新子模块的代码到最新版本

js 复制代码
 git submodule update --remote 

例如:

js 复制代码
> git submodule update --remote
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 982 bytes | 5.00 KiB/s, done.
From https://gitee.com/lucktk/go-development-practices
   2194aff..776d637  master     -> origin/master
Submodule path 'xss/practices': checked out '776d637f740087b2ceabaedcc8ead7692816d058'

> git submodule
+776d637f740087b2ceabaedcc8ead7692816d058 xss/practices (remotes/origin/HEAD)

可见,对应的版本号已经更新了。 以上方法等同于: 切换到子模块的目录,执行checkout的命令

js 复制代码
cd ./xss/practices
git checkout 776d637f740087b2ceabaedcc8ead7692816d058

4. 修改

可以切换到子模块的目录下,使用git add、commit、push操作进行修改 例如,这里增加了READ.arabic.md的文件

js 复制代码
> git status
HEAD detached at 776d637
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        READ.arabic.md

接着,我们通过Git命令来提交、推送

js 复制代码
> git add .
> git status
HEAD detached at 776d637
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   READ.arabic.md

> git commit -m "add alabic.md"
[detached HEAD e4a9ac2] add alabic.md
 1 file changed, 1 insertion(+)
 create mode 100644 READ.arabic.md
 
> git push origin HEAD:master
...

> git status
HEAD detached from 776d637
nothing to commit, working tree clean

5. 删除

可以使用以下命令进行删除子模块(参数 -f,子模块工作区内即使有本地的修改,也会被移除)

js 复制代码
git submodule deinit <Git Submodule>

例如:

js 复制代码
git submodule deinit -f  xss/practices
Cleared directory 'xss/practices'
Submodule 'xss/practices' (https://gitee.com/lucktk/go-development-practices.git) unregistered for path 'xss/practices'

此时, xss/practices目录下的文件已被清空,但.gitmodules的配置依然存在,可以执行

5.1 删除子模块配置

js 复制代码
 git rm <submodule_path> 

例如

js 复制代码
>  git rm xss/practices
error: the following file has changes staged in the index:
    xss/practices
(use --cached to keep the file, or -f to force removal)

s>  git rm xss/practices -f
rm 'xss/practices'

5.2 删除子模块的相关目录

js 复制代码
 rm -rf <submodule_path>

5.3 最后,提交更改

js 复制代码
git commit -m "Remove submodule <submodule_path>"

这样就是,再执行查看命令

js 复制代码
> git submodule 
>

可见已经删除成功。

参考

相关推荐
Flynt18 小时前
HN 382分热文里的终端小技巧,我实测了一遍,git pickaxe 最值
git·命令行
横木沉1 天前
IntelliJ IDEA 无法识别 Git:Git is not installed 问题解决
java·git·github·intellij-idea
为你学会写情书1 天前
Git 学习指南:从日常命令到多人协作与提交规范
git
东小黑1 天前
Git 仓库操作与 VSCode 使用指南
git·vscode·gitee
拼图2092 天前
Git常用语法
分布式·git·学习
ShineWinsu2 天前
对于Git:基础操作的超详细保姆级解析
linux·c++·git·面试·备份·管理·版本控制器
沉迷...2 天前
Git skip-worktree 使用笔记(本地忽略配置文件)
javascript·git
解道Jdon2 天前
JDK 27发布:紧凑对象头、G1默认、量子安全TLS
ide·windows·git·svn·eclipse·github·visual studio
天天喝旺仔2 天前
Git 内部原理深度解析:从 blob/tree/commit 对象到 packfile 与垃圾回收
数据结构·数据库·git·算法·哈希
艾莉丝努力练剑2 天前
【Git:综合复盘】Git 原理与使用
大数据·人工智能·git·elasticsearch·面试