--- description: Sync current branch into trunk via rebase + fast-forward argument-hint: "[trunk] [test-command]" --- Sync current branch into trunk. Linear history only. No merge commits. Never force-push trunk. Args: if `$1` is branch/ref → trunk, `${@:2}` → tests. Else infer trunk, `$ARGUMENTS` → tests. 1. Inspect - Run: `git status --short --branch`, `git branch --show-current`, `git remote -v`, `git worktree list --porcelain`, `git branch -vv`. - Stop on: non-git, detached HEAD, merge/rebase in progress, dirty index/worktree, dirty trunk worktree. - Record: branch, upstream, remote, trunk, HEAD. 2. Find trunk - Prefer arg. - Else infer: upstream/base metadata → remote HEAD → `main` → `master` → `trunk` → `develop`. - Ambiguous → ask. - If trunk checked out in worktree, use that worktree for trunk ops. 3. Fetch/update trunk - `git fetch --prune --tags `. - If trunk worktree: `git -C pull --ff-only`. - Else if local trunk exists: `git fetch :` (no `+`). - Else create from remote: `git branch --track /`. - Trunk divergence → stop. 4. If already on trunk - `git pull --ff-only`. - Run tests if given/obvious. - Push if ahead: `git push `. - Report. 5. Rebase branch - Check diff/log: `git log --oneline --left-right --cherry-pick ...HEAD`, `git diff --stat ...HEAD`. - If needed: `git rebase `. - Conflict → stop; show status/files/next commands. - Verify branch still has intended commits/diff. 6. Test - Run supplied test cmd, else cheap obvious checks from project files. - Failure → stop. No merge/push. 7. Fast-forward trunk - Require: `git merge-base --is-ancestor HEAD`. - If trunk worktree: `git -C merge --ff-only `. - Else: `git update-ref refs/heads/ HEAD `. 8. Push - `git push `. - Rejected → fetch, re-evaluate, rebase once if safe, retest critical checks, retry. - Never force-push trunk. Report: trunk/remote, branch old→new SHA, rebase yes/no, tests, pushed SHA, follow-up.