管理git的submodule

添加子仓库

在主仓库需要添加submodule的地方新建:

1
git submodule add https://github.com/xxx/sub_xxx.git

注:-b your_branch可选

更新主仓库

1
2
3
git add .
git commit -m "add submodule"
git push

自动更新主仓库

打开github,创建新token

image-20230425114629665

记录下这个token,只会显示一遍

在需要自动更新的submodule中点击

image-20230425114446598

添加secret

image-20230425114520685

回到仓库的Action,点击set up a workflow yourself ->,随意起名,粘贴下述代码中的repository和branches:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
name: Send submodule updates to parent repo

on:
push:
branches:
- main

jobs:
update:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
with:
repository: xxx/parent_xxx
token: ${{ secrets.PRIVATE_TOKEN_GITHUB }}

- name: Pull & update submodules recursively
run: |
git submodule update --init --recursive
git submodule update --recursive --remote
- name: Commit
run: |
git config user.email "actions@github.com"
git config user.name "GitHub Actions - update submodules"
git add --all
git commit -m "Update submodules" || echo "No changes to commit"
git push

image-20230425115003220

使用主仓库

1
git clone --recursive https://github.com/xxx/parent_xxx

若子仓库有更新

1
2
3
4
# 拉取对应的分支
git pull
# 更新对应代码
git submodule update --remote --recursive