问题描述
我的 github 存储库中有两个分支 - master
和 dev
分支.我有一个要求,我需要在以下条件下将 master
分支合并到 dev
分支:
I have two branches in my github repository - master
and dev
branch. I have a requirement where I need to merge master
branch into dev
branch under below conditions:
- 一旦 PR 直接合并到 master 分支,我需要自动将 master 分支合并回 dev 分支.
- 每当有人直接向 master 分支添加提交时,我需要自动将
master
分支合并回dev
分支.
- Once a PR is merged into master branch directly then I need to merge master branch back to dev branch automatically.
- Whenever someone adds a commit into master branch directly then I need to merge
master
branch back todev
branch automatically.
这有可能做到吗?我相信我们可以使用 git Hooks 完成这项工作,但我不知道该怎么做.有人可以举例说明如何实现这一目标吗?
Is this possible to do by any chance? I believe we can make this work with git Hooks but I am not sure how to do it. Can someone provide an example on how to achieve this?
我在网上阅读了它,看起来我可以使用 post-receive
钩子,其中包含以下内容,但我很困惑只有当有人向 master 分支或任何 PR 合并到主分支?这也是正确的方法吗?
I read it online and looks like I can use post-receive
hook with below content in it but I am confuse on how to do this only if someone adds a commit to master
branch or any PR gets merged to master branch? Also is this even the right way to do it?
git checkout master
git pull
git checkout dev
git pull
git merge master --no-ff --no-edit
git push
我明白,由于合并冲突,它可能并不总是可行,但如果可以,我们希望它自动发生.
I appreciate that it may not always be possible, owing to merge conflicts, but if it is possible, we'd like it to happen automatically.
更新
在阅读了有关 Github Actions
的更多信息后 - 我在我的 git 根文件夹中创建了一个类似 .github/workflows/merge-back-to-dev.yml
的文件具有以下内容的存储库.这看起来对吗?我是否需要所有这些字段,例如 runs-on
?
After reading more about Github Actions
- I created a file like this .github/workflows/merge-back-to-dev.yml
in my root folder of git repository with below content. Does this look right? Do I need all these fields like runs-on
?
name: 'Nightly Merge'
on:
push:
branches:
- master
jobs:
nightly-merge:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Nightly Merge
uses: robotology/[email protected]
with:
stable_branch: 'master'
development_branch: 'dev'
allow_ff: false
所以现在有了这个更改,每当我直接向 master
分支添加提交或任何 PR 直接合并到 master
分支时,master 分支将被合并到 dev 分支自动对吗?
So now with this change, whenever I add a commit to master
branch directly or any PR gets merged to master
branch directly then master branch will be merged to dev branch automatically right?
推荐答案
对于私有 GHE (GitHub Enterprise) 服务器,仅 pre-支持接收挂钩,这不是您所需要的(接收后挂钩就可以了)
For a private GHE (GitHub Enterprise) server, only pre-receive hooks are supported, which is not what you need (post-receive hooks would be fine)
由于 OP 也有 GitLab 服务器,因此 gitab-ci.yml
可以解决问题并执行合并.
有点像这个要点::p>
Since the OP also have a GitLab server, a gitab-ci.yml
could do the trick and perform the merge.
A bit like this gist:
merge_back:
stage: merge back master to dev
only:
- master
script:
- git config --global user.email "$GIT_USER_EMAIL"
- git config --global user.name "$GIT_USER_NAME"
- git switch dev
- git merge $CI_COMMIT_SHA --no-commit --no-ff
原始答案(当我以为是关于 github.com 时)
Original answer (when I thought it was about github.com)
这不会使用钩子,而是 GitHub Actions,这有助于自动化在 GitHub 上执行的流程端(与在客户端执行的钩子相反)
That would not be using hooks, but GitHub Actions, which helps automate a process executed on GitHub side (as opposed to hooks, which are executed on client side)
您可以使用 on.<push|pull_request>.<branches|tags>
进入你的 .github/workflows/myscript.yml
(你可以用更夸张的名称替换myscript",例如 merge-back-to-dev.yml
)
You can using the on.<push|pull_request>.<branches|tags>
into your .github/workflows/myscript.yml
(you can replace "myscript" by a more expessive name, like merge-back-to-dev.yml
)
您可以使用 nightly-merge
,它会自动将稳定分支(master
或 main
)合并到开发分支中.除了你的情况,你会在推送事件中明确调用它.
You can use an action like nightly-merge
, which automatically merge the stable branch (master
or main
)into the development one. Except in your case, you would call it explicitly on push event.
这篇关于在某些条件下使用钩子自动将一个分支合并到另一个基础上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!