2 +description: Sync current branch into trunk via rebase + fast-forward
3 +argument-hint: "[trunk] [
test-command]"
6 +Sync current branch into trunk. Linear history only. No merge commits. Never force-push trunk.
8 +Args: if `$1` is branch/ref → trunk, `${@:2}` → tests. Else infer trunk, `$ARGUMENTS` → tests.
11 + - Run: `git status --short --branch`, `git branch --show-current`, `git remote -v`, `git worktree list --porcelain`, `git branch -vv`.
12 + - Stop on: non-git, detached HEAD, merge/rebase in progress, dirty index/worktree, dirty trunk worktree.
13 + - Record: branch, upstream, remote, trunk, HEAD.
17 + - Else infer: upstream/base metadata → remote HEAD → `main` → `master` → `trunk` → `develop`.
19 + - If trunk checked out in worktree, use that worktree for trunk ops.
21 +3. Fetch/update trunk
22 + - `git fetch --prune --tags <remote>`.
23 + - If trunk worktree: `git -C <wt> pull --ff-only`.
24 + - Else if local trunk exists: `git fetch <remote> <trunk>:<trunk>` (no `+`).
25 + - Else create from remote: `git branch --track <trunk> <remote>/<trunk>`.
26 + - Trunk divergence → stop.
28 +4. If already on trunk
29 + - `git pull --ff-only`.
30 + - Run tests if given/obvious.
31 + - Push if ahead: `git push <remote> <trunk>`.
35 + - Check diff/log: `git log --oneline --left-right --cherry-pick <trunk>...HEAD`, `git diff --stat <trunk>...HEAD`.
36 + - If needed: `git rebase <trunk>`.
37 + - Conflict → stop; show status/files/next commands.
38 + - Verify branch still has intended commits/diff.
41 + - Run supplied test cmd, else cheap obvious checks from project files.
42 + - Failure → stop. No merge/push.
44 +7. Fast-forward trunk
45 + - Require: `git merge-base --is-ancestor <trunk> HEAD`.
46 + - If trunk worktree: `git -C <wt> merge --ff-only <branch-or-sha>`.
47 + - Else: `git update-ref refs/heads/<trunk> HEAD <old-trunk-sha>`.
50 + - `git push <remote> <trunk>`.
51 + - Rejected → fetch, re-evaluate, rebase once if safe, retest critical checks, retry.
52 + - Never force-push trunk.
54 +Report: trunk/remote, branch old→new SHA, rebase yes/no, tests, pushed SHA, follow-up.