---
name: wrap
description: End-of-session git wrap-up that creates PRs for unpushed work, cleans up merged branches, and returns to the default branch. Use when the user says "wrap", "wrap up", "eod", "done for now", "clean up branches", or is finishing a work session.
disable-model-invocation: true
argument-hint: "[optional: specific repo path]"
---

# Wrap — End-of-Session Git Cleanup

This skill handles the "I'm done working" moment by tidying up git state: ensuring work-in-progress gets a PR, and cleaning up after merged PRs.

## Step 0 — Detect the default branch

Determine the repo's default branch dynamically — don't assume `main`:

```bash
git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@'
```

If that fails (e.g. shallow clone), fall back to:

```bash
gh repo view --json defaultBranchRef --jq '.defaultBranchRef.name'
```

Store the result as `DEFAULT_BRANCH` and use it throughout.

## Step 1 — Check current branch status

1. Get the current branch:
   ```bash
   git branch --show-current
   ```

2. If on the default branch, skip to Step 2.

3. If on a feature branch:
   - Check for uncommitted changes (`git status --porcelain`). If there are changes, ask the user if they want to commit first.
   - Check for unpushed commits:
     ```bash
     git log @{u}.. --oneline 2>/dev/null || git log origin/$DEFAULT_BRANCH.. --oneline
     ```
     If there are unpushed commits, push them.
   - Check if a PR already exists:
     ```bash
     gh pr view --json state,url 2>/dev/null
     ```
     - **No PR exists** and there are commits ahead of the default branch: offer to create one with `gh pr create`. Always confirm with the user before creating — never auto-create.
     - **PR exists and is open**: tell the user (show URL).
     - **PR exists and is merged**: note it for cleanup in Step 2.

## Step 2 — Check for merged PRs and clean up

1. List recently merged PRs authored by the user:
   ```bash
   gh pr list --author @me --state merged --limit 10 --json number,title,headRefName,mergedAt
   ```

2. Check which of those branches still exist locally:
   ```bash
   git branch --list
   ```

3. For each local branch whose PR has been merged:
   - Show the user which branches will be cleaned up and **ask for confirmation before deleting**. Never auto-delete without the user's approval.
   - Delete the approved branches safely:
     ```bash
     git branch -d <branch>
     ```
     Use `-d` (not `-D`) so git refuses if there are unmerged changes.
   - Prune stale remote tracking refs:
     ```bash
     git remote prune origin
     ```

4. Switch to the default branch and pull:
   ```bash
   git checkout $DEFAULT_BRANCH && git pull
   ```

## Output

Present a clear, concise summary:

```
Wrap-up summary:
- Branch: <what branch you were on>
- PR: <created / already open at URL / not needed>
- Cleaned up: <list of deleted branches, or "none">
- Now on: $DEFAULT_BRANCH (up to date)
```

## Notes

- If `gh` CLI is not available or not authenticated, fall back to git-only operations and tell the user to handle PRs manually.
- Respect CLAUDE.md rules — never commit directly to main.
- If the user provides `$ARGUMENTS` with a repo path, `cd` there first.
