DEV Community

Bharathvaj
Bharathvaj

Posted on • Originally published at bharathvaj.me on

How to keep two branches in sync on Github

GitHub Actions allows you to automate, customize and execute your software development workflows inside your repository.

Let’s say we have two branches master and develop in our repositories following GitFlow methodology. We often encounter a situation when a hotfix is merged directly to master then those changes has to be reverse merged to develop else they will not get reflected in the develop branches thereby creating an inconsistency.

We can set up a workflow using Github Actions to keep two branches in sync. To do so, we only need repositories enabled with GitHub Action workflows.

At the time of write, GitHub Actions are free for public repositories and have a limit of 2000 mins for a free account.

Let’s get into action,

  • Create a hidden folder .github (dot GitHub) at the root of the repository.
  • Now create a workflows folder under .github. This holds all the action workflows configured for the repository.

  • Create the following sync-develop.yml inside workflows folder.
name: Sync Back to Develop

on:
  push:
    branches:
      - master

jobs:
  sync-branches:
    runs-on: ubuntu-latest
    name: Syncing branches
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Set up Node
        uses: actions/setup-node@v1
        with:
          node-version: 12
      - name: Opening pull request
        id: pull
        uses: tretuna/sync-branches@1.2.0
        with:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
          FROM_BRANCH: 'master'
          TO_BRANCH: 'develop'
Enter fullscreen mode Exit fullscreen mode

Here we are creating a Sync Back to Develop workflow that runs the sync-branches job when changes are pushed to master remote branch. We won’t be getting into details about the configuration file as this beyond our article’s scope. To learn more, you can get started with the official docs.

We are making use of tretuna/sync-branches action. This action provides options which allows to configure the branch names and details of the PR raised.

  • All set. Let’s save these changes and push them to remote master branch. Now we can see the action getting triggered under the Actions tab of GitHub GUI.

  • This tiny workflow might run for 1-2 mins. Once done, you can see a PR being raised against develop. You can manually merge this PR or have another action Auto Merge PR to automatically merge this.

Crazy Stuff. Now bots can raise PR and merge one based on conditions without manual intervention.

We have now successfully automated the workflow. We have merely touched the surface, we can do more with GitHub Actions. Sky is the limit for ideas.

Happy Solving!.

Reference

Top comments (0)