Git Submodule
golang 项目中依赖如果无法使用 go mod 导入远程库,可以考虑使用 git submodule + go mod replace 进行管理。
1
2
3
4
5
6
|
# 添加一个submodule
git submodule [--quiet] add [-b <branch>] [-f|--force] [--name <name>]
[--reference <repository>] [--] <repository> [<path>]
# 示例
git submodule add -b v1.0.0 -f --name gosdk -- https://github.com/QQGoblin/go-sdk dependency/gosdk
|
执行上述命令后,子模块的代码会被 checkout 到 dependency/gosdk 目录中。 同时当前目录生成 .gitmodules 文件,如下:
1
2
3
4
|
[submodule "gosdk"]
path = dependency/gosdk
url = https://github.com/QQGoblin/go-sdk
branch = master
|
用户通过 git clone 包含子模块的项目时,子模块相应目录下不会包含代码。用户需要执行以下命令,读取 .gitmodules 中的信息将子模块代码下载到本地。
1
2
3
4
5
6
7
|
# 注册 .gitmodules 中的模块
git submodule init -- [path]
git submodule update --remote
# 切换分支
git submodule set-branch -b [branch] -- [path]
git submodule update --remote
|
其他相关命令:
1
2
3
4
5
6
7
8
9
10
11
12
|
git submodule [--quiet] add [-b <branch>] [-f|--force] [--name <name>]
[--reference <repository>] [--] <repository> [<path>]
git submodule [--quiet] status [--cached] [--recursive] [--] [<path>...]
git submodule [--quiet] init [--] [<path>...]
git submodule [--quiet] deinit [-f|--force] [--] <path>...
git submodule [--quiet] update [--init] [--remote] [-N|--no-fetch]
[-f|--force] [--rebase] [--reference <repository>]
[--merge] [--recursive] [--] [<path>...]
git submodule [--quiet] summary [--cached|--files] [(-n|--summary-limit) <n>]
[commit] [--] [<path>...]
git submodule [--quiet] foreach [--recursive] <command>
git submodule [--quiet] sync [--] [<path>...]
|