简体   繁体   中英

Can GitHub automatically merge branches?

We want to automatically merge out from master to another long-lived branch whenever any changes are committed to master (at the moment, this is a manual process and people forget)

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.

Is this possible?

据我所知,您可以在git-automatic-merges-with-server-side-hooks(包括脚本)中找到有关此问题的非常不错的博客文章。

Is available since 2020-12-16

The native GitHub Auto-Merge was introduced on GitHub Universe 2 days ago.

How to active them? Go to Settings of your repository, eg https://github.com/rectorphp/rector/settings , then ↓

在此处输入图片说明


Tip: do you want to prevent merged branch piling up? Enable autobranch removal too


Source

While it might not be solely what you need to you for your use case, it can be used with GitHub Actions to get there more easily.

I've been using https://github.com/marketplace/actions/merge-pull-requests for a while and it works pretty well. In that page you have the instructions on how to use it.

If you need more specific workflows, you can try https://mergify.io/ also.

You can use GitHub Actions since Aug 13, 2019

This method will merge your branch with the master without the requirement to create pull requests manually.

Just create .github/workflows/automerge.yml file in your repo with this content:

name: Automerge

on:
  workflow_dispatch:
  schedule:
    # You can setup schedule here
    - cron: '0 0 * * *'

env:
  # replace "github_username" with your GitHub username
  # replace "github.com/username/repo.git" with your GitHub repo path
  # do NOT replace ${{secrets.GITHUB_TOKEN}}, GitHub will take care of it
  MY_REPO: https://github_username:${{secrets.GITHUB_TOKEN}}@github.com/username/repo.git

  # replace "long-lived_branch_name" with your branch name
  MY_BRANCH: long-lived_branch_name

  # replace it with the path to master repo
  MASTER_REPO: https://github.com/username/master_repo.git

  # replace "master" with your master branch name
  MASTER_BRANCH: master

jobs:
  merge:
    runs-on: ubuntu-latest

    steps:
    - name: Merge with master
      run: |
        git clone ${{env.MY_REPO}} -b ${{env.MY_BRANCH}} tmp
        cd tmp
        git config user.name "Automerge Bot"
        git config user.email "bot@example.com"
        git config pull.rebase false
        git pull ${{env.MASTER_REPO}} ${{env.MASTER_BRANCH}}
        git push

  • replace "github_username" with your GitHub username
  • replace "github.com/username/repo.git" with your GitHub repo path
  • replace "long-lived_branch_name" with your branch name
  • replace "master" with your master branch name
  • edit "cron" line to adjust the schedule

Also, don't forget to enable this workflow on the "Actions" page of your repo. You can run it manually too. You'll receive an e-mail from GitHub if merge was failed.

您现在可以使用Github操作https://github.com/features/actions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM