DEV Community

Nehemiah Cheburet
Nehemiah Cheburet

Posted on

How to Auto-Merge Branches on Every Push Using GitHub Actions

We’ve all been there: you are working out of a develop branch, things are moving fast, and you want your production-ready main branch to stay instantly synced without the hassle of opening, reviewing, and manually clicking "Merge" on a Pull Request every single time.

If you are working on a solo project or running an internal staging-to-production pipeline where strict PR reviews aren't necessary, you can fully automate this with a lightweight GitHub Actions workflow.

In this quick guide, we’ll set up a workflow that automatically merges develop into main every single time you push code.

Step 1: Create the Workflow File
GitHub Actions looks for automation scripts inside a special directory in your repository. You can create this file locally or directly through the GitHub web interface.

Create a file at this exact path:

.github/workflows/auto-merge.yml

And paste the following YAML configuration:

name: Auto Merge Develop to Main

on:
  push:
    branches:
      - develop

jobs:
  merge-to-main:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0 # Fetches full history so git can handle the merge properly

      - name: Configure Git Bot
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"

      - name: Merge develop into main
        run: |
          git checkout main
          git merge origin/develop --no-ff -m "chore: automatic merge develop to main [skip ci]"
          git push origin main
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Enter fullscreen mode Exit fullscreen mode

Note: If you are creating this via the GitHub web interface, make sure you commit this file directly to your develop branch so the trigger can find it!

Step 2: Fix the "Permission Denied" Gotcha
If you skip this step, your workflow will trigger, but it will immediately fail with a permission error. By default, GitHub Actions has read-only access to your repository. Because our script needs to write (push) code back to main, we have to elevate its permissions.

Head over to your repository on GitHub.

Click the Settings tab at the top.

On the left sidebar, click Actions > General.

Scroll all the way to the bottom to Workflow permissions.

Switch the toggle to Read and write permissions and hit Save.

Step 3: Test the Magic

To see it in action, make a small change to a file on your local develop branch, commit it, and push it up:

git add .
git commit -m "feat: testing auto merge"
git push origin develop
Enter fullscreen mode Exit fullscreen mode

Now, click on the Actions tab in your GitHub repository. You’ll see the "Auto Merge Develop to Main" workflow running. Within a few seconds, it will complete, and your main branch will be perfectly updated.

(Don't forget to run git pull on your local machines to keep everything synced!)

⚠️ A Quick Reality Check
Before you throw this into every repo you own, keep two edge cases in mind:

Merge Conflicts: If someone pushes a hotfix directly to main, this script will fail when a conflict occurs. If that happens, the automation stops safely. You'll just need to merge main back into develop manually on your machine, resolve the conflict, and push to develop again.

Branch Protection Rules: If your main branch requires signed commits, approval reviews, or passing status checks before a merge, this native Git push will be blocked by GitHub. To bypass that, you would need to generate a Personal Access Token (PAT) with repository write access and substitute it for the default ${{ secrets.GITHUB_TOKEN }}.

Happy automating! Drop a comment below if you run into any issues setting this up.

Top comments (0)