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 
>

可见已经删除成功。

参考

相关推荐
ganshenml9 小时前
【GIT】Git 本地无法识别远程分支的原因与解决方法 not a valid ref
大数据·git·elasticsearch
lizz6669 小时前
在Git提交中,常见的提交类型(基于Angular提交规范,已被广泛采用)
git
涵涵(互关)9 小时前
git基础操作(按图一步一步来,有案例)
git
我是苹果,不是香蕉10 小时前
git remote报错解决办法
git
SHIPKING39311 小时前
【git命令操作指南】
git
LT_102912 小时前
如何初始化一个本地的项目到远程git仓库?
git·源代码管理
小付爱coding12 小时前
Claude Code安装教程【windows版本】
java·git·python
BIBI204913 小时前
Windows 下 Git 常规操作教程:命令行与 TortoiseGit
windows·git·tortoisegit·配置·版本控制·入门指南
The Straggling Crow16 小时前
熟练版本控制 (Git)、CI/CD 流程。
git·elasticsearch·ci/cd
海绵宝宝_18 小时前
Copilot 一键生成中文 Git Commit Message
git