Can GitHub automatically merge branches?

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, e.g. https://github.com/rectorphp/rector/settings, then ↓

enter image description here


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.


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 "[email protected]"
        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.


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.