How to keep two branches in sync on Github?

date
Apr 2, 2021
slug
keep-two-branches-in-sync
status
Published
tags
git
github
automation
summary
Learn how to keep two branches in sync on Github.
type
Post
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.
notion image
  • Create the following sync-develop.yml inside workflows folder.
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.
notion image
  • 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.
notion image
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!.

References


© Bharathvaj Ganesan 2020 - 2023