问题
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.
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
At this point an automated build and deploy from C is executed.
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?
Edit: 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.
回答1:
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.
- Go to each sub module repo.
Create file .git/hooks/post-commit with following content:
#!/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
Make it executable (not required on Windows):
$ chmod +x .git/hooks/pre-commit
来源:https://stackoverflow.com/questions/54895559/git-hook-to-update-superproject-on-submodule-change