Snippet: GitHub workflow to sync downstream forks

By Antonio Cheong on on Permalink.

Had enough of going through the all the personal forks I have of projects where I containerize them and make personal preference changes that will never make it into upstream. Here's a dead simple workflow for rebase and force push.

name: Sync Fork with Upstream (Force Push)

on:
  schedule:
    - cron: '0 1 * * *'
  workflow_dispatch:

# ============================================
# CONFIGURATION - Edit these for your fork
# ============================================
env:
  UPSTREAM_REPO: 'https://github.com/iv-org/invidious-companion.git'
  UPSTREAM_BRANCH: 'master'
  FORK_BRANCH: 'master'
# ============================================

jobs:
  sync:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          ref: ${{ env.FORK_BRANCH }}
          fetch-depth: 0

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

      - name: Add upstream and fetch
        run: |
          git remote add upstream ${{ env.UPSTREAM_REPO }}
          git fetch upstream

      - name: Rebase fork onto upstream
        id: rebase
        run: |
          git checkout ${{ env.FORK_BRANCH }}

          # Attempt rebase
          if git rebase upstream/${{ env.UPSTREAM_BRANCH }}; then
            echo "result=clean" >> $GITHUB_OUTPUT
          else
            echo "result=conflict" >> $GITHUB_OUTPUT
            git rebase --abort || true
          fi

      - name: Force push if clean
        if: steps.rebase.outputs.result == 'clean'
        run: |
          git push --force-with-lease origin HEAD:refs/heads/${{ env.FORK_BRANCH }}
          echo "Rebase clean — ${{ env.FORK_BRANCH }} updated from upstream/${{ env.UPSTREAM_BRANCH }}."

      - name: Create or update conflict issue
        if: steps.rebase.outputs.result == 'conflict'
        uses: peter-evans/create-issue-from-file@v5
        with:
          title: 'Upstream Sync Conflict'
          content-filepath: .github/conflict-notice.md
          labels: upstream-sync, conflict

You need to create a .github/conflict-notice.md file with just some text/instructions in case rebase fails. It'll open an issue informing you about it.