Merge

From drn/dots · View interactive version

Merge is an AI agent skill from the GitHub repository drn/dots. Merge current branch to the default branch via GitHub PR merge. Use when ready to merge a PR or land a branch.

How to use this skill

Skill file

Context

Phase Protocol

This skill participates in a phase chain. Read ~/.claude/skills/_shared/resources/phase-protocol.md for the full protocol.

Before merging: Read the latest .context/phases/ship-*.md for the PR number/URL if available.

After merge completes: Write a land-{ts}.md artifact to .context/phases/ (create with mkdir -p .context/phases). The Detail section should include the merge commit SHA and PR URL. Optionally archive all phase artifacts: mkdir -p .context/phases/archive && mv .context/phases/*.md .context/phases/archive/.

Your task

Merge the current branch into the default branch via GitHub PR merge. An existing PR is NOT required — one will be created if needed.

Step 0: Preflight — ahead/behind check

Before committing anything, verify the branch is in a mergeable state.

Run:

TARGET=$(git remote | grep -q '^upstream$' && echo upstream || echo origin)
git fetch "$TARGET" >/dev/null 2>&1 || true
DEFAULT_BRANCH=$(git branch -r 2>/dev/null | grep -oE "${TARGET}/(main|master)" | head -1 | sed "s|${TARGET}/||")
[ -z "$DEFAULT_BRANCH" ] && DEFAULT_BRANCH="master"
AHEAD=$(git log "$TARGET/$DEFAULT_BRANCH..HEAD" --oneline 2>/dev/null | wc -l | tr -d ' ')
BEHIND=$(git log "HEAD..$TARGET/$DEFAULT_BRANCH" --oneline 2>/dev/null | wc -l | tr -d ' ')
DIRTY=$(git status --short 2>/dev/null | wc -l | tr -d ' ')
echo "ahead=$AHEAD behind=$BEHIND dirty=$DIRTY default_branch=$DEFAULT_BRANCH"

Decide based on the output:

These ahead/behind counts are the authoritative signal for what the branch will land. The injected context block reflects them: the "Commits vs <base>" lines use a two-dot range (<base>..HEAD), which lists exactly the commits this branch adds, and the "Diff stat vs <base> (branch-only)" lines use a three-dot diff (<base>...HEAD, from the merge-base), which shows only this branch's own file changes even when behind > 0. Both are already scoped to the branch — neither inflates when behind.

Caveat for manual checks: if you run a plain two-dot diff yourself (git diff <base>..HEAD --stat) while the branch is behind, it folds base-ahead history in as inverted changes and lists many files unrelated to this branch. That is expected, not a problem with the branch — Step 3 rebases the branch onto the current <base> tip, after which the two-dot and three-dot diffs converge. Trust the Step 0 counts and the three-dot diff stat above, not a raw two-dot diff.

Step 1: Commit uncommitted changes

If git status shows uncommitted changes, stage and commit them with an appropriate message. Skip if working tree is clean.

Step 2: Analyze commits and craft PR metadata

Review all commits and the full diff since the branch diverged from the default branch. Determine:

If there are no prior commits (only what was committed in step 1), base the summary on that commit.

Step 3: Run the merge script

Resolve the script path — use the first that exists:

  1. ~/.claude/skills/merge/scripts/merge.sh (deployed via symlink)
  2. agents/skills/merge/scripts/merge.sh (repo-relative, for development/workspaces)
bash <script-path> [--method <squash|merge|rebase>] [--keep-branch] [--no-wait-checks] "<title>" "<body>"

The script fails closed on CI. It waits for pending checks (default 1200s, override with the MERGE_CHECK_TIMEOUT env var in seconds) and refuses to merge when a check is failing. When the repo has GitHub-required checks, those gate. When none are required, every check on the PR gates, except names matching MERGE_IGNORE_CHECKS (case-insensitive regex, default coderabbit). If no checks have registered yet, it polls for up to MERGE_CHECK_GRACE seconds (default 60) before concluding there are none. A gh error while reading checks is retried until the timeout, never read as zero checks. Because the gate runs before the merge tiers, the --admin tier never runs while CI is pending or failing. Pass --no-wait-checks only when the user explicitly wants to skip that gate.

Choosing a merge method

The script defaults to squash (collapses all commits into one). Pick a different method when squash would lose intentional structure:

Heuristic: if the branch has more than one commit AND the commit messages look distinct rather than incremental ("WIP", "fix typo"), confirm with the user before using the default squash. The deliberate-split case is common after /squash.

Post-merge worktree state

After a successful squash merge, the script auto-switches the current worktree to the default branch (master/main). This prevents the next agent or follow-up task in this worktree from committing against the now-divergent feature branch — squash collapses all commits into one new commit on master, so the old feature branch HEAD no longer shares history with the merged result.

If the default branch is checked out in another worktree, the script falls back to git checkout --detach <remote>/<default>, leaving this worktree on a detached HEAD at the new tip (create a new branch before committing). If that also fails (e.g., uncommitted changes), it prints a loud warning listing both options — git checkout --detach <remote>/<default>, or git reset --hard <remote>/<default> to reuse the branch name (discards the branch commits, so only after the merge landed) — rather than aborting, since the merge itself already succeeded.

Review-required repos: automatic `--admin` escalation

The script handles branch protection on its own — you do not need to fall back to a manual gh pr merge --admin. When reviewDecision is REVIEW_REQUIRED or CHANGES_REQUESTED, do_merge escalates through tiers automatically:

  1. Retry the merge with --admin (branch-protection bypass; succeeds when you hold admin on the repo). A successful admin merge prints WARNING: merged via --admin (branch protection bypassed) so the bypass is never silent.
  2. If --admin fails (lacking admin is one cause, but a pending required status check is the more common one — gh refuses an admin merge until required checks resolve), enable --auto (auto-merge) so the PR lands once checks pass. The real admin error is printed (Admin merge failed: …), not swallowed.
  3. Only if both fail does it exit 4 — and the exit-4 message includes the actual admin-merge error so the cause is visible.

On a repo where you hold admin, /merge lands the PR through the review gate itself — never substitute a raw gh pr merge --admin, which skips the post-squash worktree auto-switch and strands you on the merged feature branch. A plain non-blocked PR merges directly; the admin tier only kicks in when review is actually blocking.

Handle the exit code

1. Read the conflicting files from stderr

2. Open each file, resolve the conflict

3. git add resolved files

4. git rebase --continue

5. Repeat if more conflicts

6. Re-run: bash <script-path> --skip-rebase [--method <method>] "<title>" "<body>"

7. Show the output block verbatim

Special case: "distinct types" conflicts. If the rebase reports conflicts because the branch converted regular files to symlinks (or vice versa) while master independently edited those same files, do NOT naively resolve with --theirs or --ours. Both sides "win" — the branch's symlink target was sourced from old master content, so keeping the branch's version silently loses master's content updates.

Recovery pattern:

1. git rebase --abort.

2. Drop the symlink-conversion commit from the branch (e.g., git rebase -i master and drop it, or git reset --soft <sha-before-symlink-commit> then re-stage selectively).

3. Rebase the remaining commits onto master cleanly.

4. Rebuild the symlink-conversion commit fresh against current master — re-source the symlink target's content from the current master file, not from the original AGENTS.md content captured on the branch.

5. Re-run the merge script with --skip-rebase.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.