Automate GitHub Repository Sync Using GitHub Actions and PAT
In many development workflows, keeping multiple repositories in sync is a critical task—especially when managing forked repositories or mirroring codebases. In this guide, you'll learn how to automatically sync one GitHub repository (Repo A) to another (Repo B) using GitHub Actions and a Personal Access Token (PAT). This automation ensures that Repo B always reflects the latest commits, branches, and tags from Repo A with zero manual effort.
Step 1: Generate Github Personal Access Token
Login to Github ->Settings ->Developer Settings ->Personal Access Token ->Classic Tokens → Generate New Token
Click on Profile in top right
Click on Setting
Click on Developer Settings
Click on Tokens (Classic)
Generate new token
add Token name, Description and then Generate it and Save it.
Now Github Personal Token has been created.
Step 2: Add github Secrets
GitHub repository:
Settings ➔ Secrets and Variables ➔ Actions ➔ New repository secret
Add these secrets:
Secret Name: PERSONAL_ACCESS_TOKEN (Your Personal Access Token (PAT), DESTINATION_REPO (Destination repository full name (org/user + repo)
Click on Setting
Click on Actions
Then Click on Variables and then New Repository Secrets.
Add Secret name and Value and save it.
Now Secrets has been saved.
Step 3: Create a GitHub Action Workflow in Repo A
This is my Repo A. (FTPT1998)
This is my Repo B (Reposittory-B)
This will automate the sync from Repo A to Repo B.
In your Repo A (FTPT1998):
Add the following GitHub Actions workflow script: name: Sync Repo A to Repo B
name: Sync Repo A to Repo B
on:
push:
branches:
- '**'
create:
branches:
- '**'
pull_request:
types:
- closed
jobs:
sync:
if: github.event.pull_request.merged == true || github.event_name != 'pull_request'
runs-on: ubuntu-latest
steps:
- name: Checkout Repo A
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GH_TOKEN }}
- name: Push Repo A to Repo B
env:
TOKEN: ${{ secrets.GH_TOKEN }}
DESTINATION_REPO: ${{ secrets.DESTINATION_REPO }}
run: |
echo "Starting sync..."
git remote add destination https://github.com/${DESTINATION_REPO}.git
git fetch --all
echo "Setting credentials..."
git config --global user.name "GitHub Action Bot"
git config --global user.email "action-bot@example.com"
echo "Pushing all branches to destination repository..."
git push https://${TOKEN}@github.com/${DESTINATION_REPO}.git --all --force
echo "Pushing all tags to destination repository..."
git push https://${TOKEN}@github.com/${DESTINATION_REPO}.git --tags --force
echo "Repository sync completed successfully."
Now we have committed the pipeline script in source repo that is repo A.
Step 4: Verify GitHub Action Execution
Navigate to:
Monitor the workflow named "Sync Repo A to Repo B".
If everything is correctly configured:
You will see a green ✅ successful workflow.
The sync job will push your branches and tags to Repo B (Reposittory-B).
Pipeline is successful.
Step 5: Confirm the Sync in Repo B
Go to your destination repository (Repo B).
Verify that:
New branches are created.
Commits and tags are pushed successfully.
Files match Repo A.
If verified, the sync is working correctly.
We have successfully automated syncing between two GitHub repositories using GitHub Actions and Personal Access Tokens (PAT).
This setup ensures that Repo B always mirrors Repo A automatically without manual intervention.