Sync Skill
Overview
Fetch the latest origin/main, rebase this checkout onto it, commit local changes, and push. Sync is two-way: remote commits land on local main, and local commits land on the remote.
Use Claude Code's Bash tool for the Git operations below, running from the repository root.
If the user provided a commit message (for example through the slash-command argument), use that message. Otherwise, generate one from the staged diff.
Steps
Phase 1: Fetch and integrate origin/main
- Run
git status --shortandgit rev-parse --abbrev-ref HEAD. - Run
git fetch origin main. This updatesorigin/mainand does not change the working tree. - If the current branch is not
main, report the branch name and thatorigin/mainwas fetched. Skip the rebase in this phase. Continue with Phase 2 and Phase 3 for the current branch. - On
main, record both directions:
- Ahead:
git rev-list --count origin/main..HEAD - Behind:
git rev-list --count HEAD..origin/main
- If behind is
0, continue to Phase 2. - If behind is greater than
0andgit status --shortis empty, rungit pull --rebase origin main. - If behind is greater than
0and the working tree is dirty, rungit stash push -u -m "sync", thengit pull --rebase origin main, thengit stash pop. - If the rebase or the stash pop stops with conflicts, stop. Report the conflicted files. Leave a saved
syncstash in place. Do not commit, push, abort, or force-push.
Phase 2: Commit any uncommitted changes
- Run
git status --shortagain. - If there are no uncommitted changes, skip to Phase 3.
- Run
git add -Ato stage ALL changes (new, modified, and deleted files). - Generate a concise commit message that summarizes the changes:
- If the user provided a commit message, use that message.
- Otherwise, review
git diff --cached --statand write a message that summarizes the main updates. - Keep the message under 100 characters for the first line; add a blank line then a short body if needed.
- Do not add a
Co-Authored-Bytrailer. Commits use the local git user only.
- Run
git commit -m "<message>"with the generated message.
Phase 3: Push any unpushed commits
- Run
git rev-list --count @{upstream}..HEADto check how many commits are ahead of the remote.
- If the output is
0or the command errors (no upstream), also rungit log --oneline origin/main..HEADto detect unpushed commits.
- If there are unpushed commits, run
git push. - If the push is rejected because the remote has newer commits, run
git fetch origin main, thengit pull --rebase origin main, thengit pushonce more. If that second push fails, stop and report the error. - If the push fails for any other reason, stop and report the error.
- If there are no unpushed commits, report that local commits are already on the remote.
Phase 4: Report result
- Report how many commits came in from
origin/main, the number of files changed (if any), the commit hash (if any), and whether the push succeeded. - On
main, the sync is complete only when both ranges are empty:
git log --oneline origin/main..HEADgit log --oneline HEAD..origin/main
- Confirm with
git status -sb. A finishedmainsync showsmain...origin/mainwith no ahead or behind count.
CRITICAL
- NEVER skip Phase 1. Fetch
origin/maineven when there is nothing to commit or push. - NEVER skip Phase 3. Even if there are no uncommitted changes, there may be unpushed commits from earlier in the session. Always check and push if needed.
- Rebase onto
origin/main. Do not create a merge commit. - NEVER run
git push --forceorgit push --force-with-lease. - Stop on conflicts. Do not keep going after a rebase conflict or a stash-pop conflict.