$npx -y skills add heymegabyte/claude-skills --skill finishing-a-development-branchUse when implementation is complete, all tests pass, and you need to decide how to integrate the work - guides completion of development work by presenting structured options for merge, PR, or cleanup
| 1 | # Finishing a Development Branch |
| 2 | |
| 3 | Integrate finished work: verify → detect environment → choose integration → execute → clean up. |
| 4 | |
| 5 | Under `[[no-staging-doctrine]]` + `[[main-only-branch]]` the default is **merge to `main` + auto-push** — diffs clearing all gates auto-merge. Don't offer "keep the branch as-is, handle it later"; finish the work this turn. PR only when a human review is explicitly wanted. |
| 6 | |
| 7 | Announce: "Using the finishing-a-development-branch skill to complete this work." |
| 8 | |
| 9 | ## Step 1 — gate before integrating |
| 10 | |
| 11 | - Run the project test suite. Failures block — show them, fix first, do NOT integrate broken code. |
| 12 | - Full deploy + prod-E2E gate is `[[verification-loop]]` — local green ≠ done. |
| 13 | |
| 14 | ## Step 2 — detect environment |
| 15 | |
| 16 | ```bash |
| 17 | GIT_DIR=$(cd "$(git rev-parse --git-dir)" 2>/dev/null && pwd -P) |
| 18 | GIT_COMMON=$(cd "$(git rev-parse --git-common-dir)" 2>/dev/null && pwd -P) |
| 19 | ``` |
| 20 | |
| 21 | - `GIT_DIR == GIT_COMMON` → normal repo, no worktree cleanup. |
| 22 | - `GIT_DIR != GIT_COMMON`, named branch → worktree, provenance-based cleanup (Step 4). |
| 23 | - Detached HEAD → externally managed; no local-merge, no cleanup (push-as-branch or discard only). |
| 24 | - Base branch: `git merge-base HEAD main || git merge-base HEAD master`, or confirm with the user. |
| 25 | |
| 26 | ## Step 3 — integrate |
| 27 | |
| 28 | - **Merge to base (default).** `cd` to main repo root first (CWD safety), merge, re-run tests on the merged result, then Step 4 cleanup, then `git branch -d`. Always auto-push the merged base. |
| 29 | |
| 30 | ```bash |
| 31 | MAIN_ROOT=$(git -C "$(git rev-parse --git-common-dir)/.." rev-parse --show-toplevel); cd "$MAIN_ROOT" |
| 32 | git checkout <base> && git pull && git merge <feature> && <test cmd> && git push |
| 33 | ``` |
| 34 | |
| 35 | - **PR (review wanted).** `git push -u origin <feature>`. Do NOT clean up the worktree — it's needed to iterate on feedback. |
| 36 | - **Discard (abandon).** Require a typed `discard` confirmation listing branch + commits + worktree path. Then `cd` to main root, Step 4 cleanup, `git branch -D <feature>`. |
| 37 | |
| 38 | ## Step 4 — cleanup (merge + discard only; PR preserves the worktree) |
| 39 | |
| 40 | ```bash |
| 41 | WORKTREE_PATH=$(git rev-parse --show-toplevel) |
| 42 | ``` |
| 43 | |
| 44 | - Normal repo (`GIT_DIR == GIT_COMMON`) → nothing to remove. |
| 45 | - Worktree under `.worktrees/` or `worktrees/` → we own it. From main root (never from inside the worktree): `git worktree remove "$WORKTREE_PATH" && git worktree prune`. |
| 46 | - Anywhere else → harness-owned; use its exit tool (`ExitWorktree`) or leave in place. Never remove a worktree you didn't create. |
| 47 | |
| 48 | ## Ordering invariants (why the sequence is fixed) |
| 49 | |
| 50 | - Merge BEFORE removing the worktree — `git branch -d` fails while a worktree references the branch. |
| 51 | - `cd` to main root BEFORE `git worktree remove` — fails silently when CWD is inside the target. |
| 52 | - `git worktree prune` after removal self-heals stale registrations. |
| 53 | |
| 54 | ## See |
| 55 | |
| 56 | - `[[no-staging-doctrine]]` · `[[main-only-branch]]` · `[[verification-loop]]` |
| 57 | - `using-git-worktrees` — the isolation setup this finishes |
| 58 | |
| 59 | <!-- budget: ~62 --> |