Git挂钩可以在子模块更改时更新超级项目? [英] Git hook to update superproject on submodule change?
问题描述
我有三个git存储库:A
,B
和C
. C
是一个包含子模块A
和B
的超级项目,跟踪每个子模块各自的主分支.
I have three git repositories: A
, B
, and C
. C
is a superproject including submodules A
and B
, tracking each submodule's respective master branch.
更改A
或B
的主分支时,我必须在C
中执行以下操作:
When the master branch of A
or B
is changed, I must do the following in C
:
git submodule update --remote
git add A
git add B
git commit -m "Update submodule A or B"
git push
此时,将执行从C进行的自动构建和部署.
At this point an automated build and deploy from C is executed.
我正在寻找简化此过程的方法.理想情况下,对A
或B
的master分支的任何提交都会触发钩子来更新,提交和推送到C
.有什么想法可以实现这一目标吗?
I'm looking for ways to streamline this process. Ideally, any commit to the master branch of A
or B
would trigger a hook to update, commit, and push to C
. Any ideas how to achieve this?
如果有我应该了解的特定于供应商的功能,则我将Azure DevOps用于git托管,并将Azure Pipelines用于构建和部署过程.
In case there's a vendor-specific feature I should be aware of, I'm using Azure DevOps for the git hosting and Azure Pipelines for the build and deploy process.
推荐答案
You can use a git post-commit hook, when you do a commit in the submodules the hook will be executed and will do the commands.
- 转到每个子模块存储库.
-
使用以下内容创建文件 .git/hooks/post-commit :
#!/bin/sh
branch="$(git rev-parse --abbrev-ref HEAD)"
if [ "$branch" = "master" ]; then
cd {super repo location}
git submodule update --remote
git add A
git add B
git commit -m "Update submodule A or B"
git push
fi
使其可执行(在 Windows 上不是必需的):
Make it executable (not required on Windows):
$ chmod +x .git/hooks/pre-commit
这篇关于Git挂钩可以在子模块更改时更新超级项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!