Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can GitHub automatically merge branches?

Tags:

git

github

People also ask

Why GitHub Cannot automatically merge?

When you create a pull request from YourAccount\repo1 to OriginalAccount\repo1 (virtually from origin to upstream), seeing the message that you can't merge automatically means that OriginalAccount\repo1 has commits that YourAccount\repo1 doesn't have (commits that were most likely pushed after you forked).

Does git pull auto merge?

Since your local commit isn't on the remote repository yet, when git pull runs git merge origin/[branch] [branch] , it will automatically do a "recursive" merge and create a commit with the remote changes.

What is auto merging in git?

Git can handle most merges on its own with automatic merging features. A conflict arises when two separate branches have made edits to the same line in a file, or when a file has been deleted in one branch but edited in the other.

Does creating a pull request automatically merge?

With auto-merge, pull requests can be set to merge automatically when all merge requirements are met. No more waiting on slow CI jobs or tests to finish just so you can click the merge button!


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.


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 "[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.