Skip to content

About

Keeps forked Github repositories in sync with their parents

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Sync Github Forks

A vibe coded single-file Python script that keeps every GitHub fork you own in sync with its upstream parent, then reports the results to Slack.

If you maintain dozens — or hundreds — of forks, manually clicking "Sync fork" in the GitHub UI does not scale. sync-github-forks walks your forks via the GitHub GraphQL API, merges upstream changes into each default branch via the REST API, and posts a tidy summary to a Slack channel.

What it does

  1. Lists every repository you own that is a fork (paginated via GraphQL, 25 at a time).
  2. Skips forks whose upstream has not been pushed to since the last successful run (timestamp persisted in the state file) — except forks left in a merge conflict last run, which are always re-checked so an unresolved conflict can't be silently hidden by the timestamp cache.
  3. For each remaining fork, asks GitHub how many commits behind its upstream's default branch it is (the fork and upstream may use different default branch names).
  4. Calls the /merge-upstream endpoint to fast-forward the fork's default branch. A reported conflict (HTTP 409) is re-confirmed once after a short delay, since GitHub's cached merge state can be transiently stale right after an upstream push.
  5. Classifies the outcome as synced, merge conflict, transient/flaky conflict (cleared on re-check), no-op (API cache artifact), or error.
  6. Tracks conflicts across runs: reports how long each has been unresolved, and flags conflicts that cleared on their own since the last run.
  7. Auto-resolves stale conflicts. Once a fork has been in conflict for more than 15 runs, the script discards the fork's own divergent commits — resetting its default branch to the merge-base with upstream — and fast-forwards it to the latest upstream. This is automatic and destructive: the fork's commits on its default branch are permanently dropped. It only triggers after a conflict has gone unresolved across 16+ runs (about 15 hours on the hourly schedule; manual runs count too), and applies only to your own forks. Safety cap: if the fork is more than 20 commits ahead of upstream, nothing is discarded — the branch is left untouched, the conflict stays tracked, and the fork is flagged in Slack under Review Required for manual resolution.
  8. Runs hourly but posts to Slack only four times a day, at 02:50, 08:50, 14:50 and 20:50 local time. Results from the runs in between are saved and included in the next report.

Transient 429 / 5xx responses and GitHub's secondary rate limits are retried with exponential backoff, honoring Retry-After when present.

Requirements

  • uv (it fetches Python 3.13+ automatically if needed)
  • A GitHub token with repo scope (classic) or equivalent fine-grained permissions on the forks you want to sync. The script will use $GITHUB_TOKEN if set, otherwise it falls back to gh auth token.
  • A Slack incoming webhook URL.

The script is self-contained: its shebang is #!/usr/bin/env -S uv run --script, and its dependencies (httpx, rich) and Python version are declared inline via PEP 723 script metadata. uv resolves them into a cached, throwaway environment on each run — there is no project virtual environment to create or activate.

Setup

These steps install the script to ~/.local/bin and run it every hour with launchd (macOS).

1. Install uv and the script

curl -LsSf https://astral.sh/uv/install.sh | sh   # skip if uv is already installed
git clone <this-repo> && cd <this-repo>
install -d ~/.local/bin
install -m 755 sync-github-forks ~/.local/bin/sync-github-forks

To pick up changes from the repo later, run the install line again. To always run the repo copy, symlink it instead: ln -sf "$PWD/sync-github-forks" ~/.local/bin/.

2. Try a manual run

export SLACK_WEBHOOK_URL='https://hooks.slack.com/services/…'
export GITHUB_TOKEN='…'   # optional; falls back to `gh auth token`
sync-github-forks          # needs ~/.local/bin on your PATH

The first run downloads httpx and rich into uv's cache, and Python 3.13 too if it's missing. Later runs start immediately. The state directory ~/.config/sync-github-forks/ is created on the first successful run.

3. Schedule it with launchd

Copy the template, then fill it in:

cp sync-github-forks.plist.example ~/Library/LaunchAgents/com.example.sync-github-forks.plist
$EDITOR ~/Library/LaunchAgents/com.example.sync-github-forks.plist

In the copy:

  • Replace /Users/YOU with your home directory, using a full path. launchd doesn't expand ~ or $HOME.
  • Set GITHUB_TOKEN and SLACK_WEBHOOK_URL. launchd jobs don't see your shell environment or .envrc, so the plist has to set the variables itself.
  • Make sure PATH includes the directory that holds uv. The script's shebang (/usr/bin/env -S uv run --script) finds uv through PATH. Check with which uv.
  • Logs go to ~/Library/Logs/sync-github-forks/ (again as full paths).
  • Optionally rename the Label. StartCalendarInterval runs the job at :50 past every hour, which lines up with the report times; if you change it, keep a run at or shortly after each report time. If the Mac is asleep, launchd runs the job once on wake.

Because the plist holds secrets, lock it down, then load it:

mkdir -p ~/Library/Logs/sync-github-forks
chmod 600 ~/Library/LaunchAgents/com.example.sync-github-forks.plist
plutil -lint ~/Library/LaunchAgents/com.example.sync-github-forks.plist
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.example.sync-github-forks.plist

Manage the job:

launchctl kickstart -k gui/$(id -u)/com.example.sync-github-forks   # run now
launchctl print gui/$(id -u)/com.example.sync-github-forks          # status, last exit code
tail -f ~/Library/Logs/sync-github-forks/std{out,err}.log           # logs
launchctl bootout gui/$(id -u)/com.example.sync-github-forks        # unload (do this before editing the plist, then bootstrap again)

Plists other than *.plist.example are gitignored, so a filled-in copy kept in the repo won't be committed.

Logs

  • stdout.log has each run's progress summary, per-fork errors (Error: …, as each run finds them) and any failure to post to Slack.
  • stderr.log has uncaught Python tracebacks and uv's own messages.

Each run starts with the same header in both files, in local time, so a traceback in stderr.log can be matched to its run in stdout.log:

=== Run started 2026-11-30 21:09:53 -0700 ===

Other lines aren't timestamped. A uv failure before the script starts (for example, it can't install Python or the dependencies) appears in stderr.log without a header.

launchd only saves this output and doesn't alert you; Slack is the notification channel. If a report is missing from Slack, check the logs. Runs append to the same files, which are not rotated.

Configuration

Variable Required Purpose
SLACK_WEBHOOK_URL yes Incoming webhook for the report channel.
GITHUB_TOKEN no Personal access token. Falls back to gh auth token if unset (so gh must then be on the job's PATH and logged in).
XDG_CONFIG_HOME no Base directory for state; defaults to ~/.config.

For interactive use, a .envrc file works well with direnv. For launchd, set them in the plist as described above.

State

State lives in ~/.config/sync-github-forks/state.json. It holds the last-run timestamp, the forks still in conflict (with run counts), the current day's accumulated synced counts, and results waiting for the next Slack report. Each run uses it to skip forks whose upstream hasn't moved since the last run.

To force a full re-check, delete it:

rm ~/.config/sync-github-forks/state.json

If you are upgrading from the old sync_forks.py, move its state file across so conflict run counts carry over:

mkdir -p ~/.config/sync-github-forks
mv sync_state.json ~/.config/sync-github-forks/state.json

Usage

sync-github-forks

The script prints a live progress bar showing forks reviewed, synced, and skipped, and posts the report to Slack at the end.

Slack output

Reports go out at 02:50, 08:50, 14:50 and 20:50 local time, from the first run at or after each time. Nothing is posted in between, including errors, new conflicts and auto-resolved (discarded) commits. A report is posted even when nothing changed, so a missing one means something is wrong.

Each report covers everything since the previous one. Open conflicts and review-required forks are shown as they stand at report time, and repeated errors are listed once. If the post fails, the results are kept and retried on the next hourly run. The first run with no saved report history posts nothing and waits for the next report time.

The report times are set by REPORT_TIMES in the script. The per-repo synced list is rolled up per local-calendar day and included in the first report of the next day:

*GitHub Fork Sync Report*

*📅 Daily Sync Summary — 2026-06-18*
*Synced Repositories:*
• 14 commits: `some-repo`
• 3 commits: `another-repo`, `third-repo`
• 1 commit: `tiny-repo`

*Synced Repositories:* 5 commits across 2 repos

*🛑 Review Required (over 20 divergent commits — auto-resolve skipped, nothing discarded):*
• `big-fork` — 34 commits ahead of upstream after 17 runs in conflict; resolve manually

*⚠️ Merge Conflicts (Manual Resolution Required):*
• `divergent-repo` — unresolved across 4 runs (first seen 2026-06-08 09:12 UTC)

The dated Daily Sync Summary appears only in the first report of a new day (normally 02:50) and covers the previous day's syncs (each repo's commits summed across that day, grouped descending). The brief Synced Repositories line totals the syncs since the last report.

The Conflicts Cleared Since Last Report and Transient Conflicts sections are the telemetry that answers "was it really a conflict?": a repo that clears on the in-run re-check was GitHub merge-state flicker, while one that clears across runs without intervention was either resolved upstream or eventual consistency. A conflict that keeps incrementing its run count is genuine and needs manual resolution.

Automating it

The script is designed to run on a schedule. See launchd setup above; cron or GitHub Actions work too. Because state is persisted between runs, each run only does real work for upstreams that have moved since the last run, which keeps API usage low even with hundreds of forks.

Development

Run the tests (also a self-contained uv script):

./test-sync-github-forks -v

Files

  • sync-github-forks — the script (executable; dependencies declared inline and run via uv).
  • test-sync-github-forks — unit tests.
  • sync-github-forks.plist.example — launchd job template with placeholder secrets.
  • docs/superpowers/ — design specs and plans recording why features work the way they do.
  • ~/.config/sync-github-forks/state.json — state written by the script (outside the repo).

About

Keeps forked Github repositories in sync with their parents

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages