Can GitHub automatically merge branches?

GitGithub

Git Problem Overview


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 Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKramView Question on Stackoverflow
Solution 1 - GitTomas VotrubaView Answer on Stackoverflow
Solution 2 - GitClusterView Answer on Stackoverflow
Solution 3 - GitYajoView Answer on Stackoverflow