Close session

From divad12/dotfiles · View interactive version

Close session is an AI agent skill from the GitHub repository divad12/dotfiles. Use when the user asks to close, end, clean up, tear down, or finish a worktree/session after work is landed.

How to use this skill

Skill file

Close Session

Close the current worktree after confirming its work is merged. This skill is a

narrow git cleanup only; it does not write summaries or clean runtime state.

Prerequisites

You must be inside a worktree, not the target repo/worktree. If you're already

in the target repo, tell the user there's nothing to close.

Steps

1. Gather session info

Set TARGET_BRANCH to the branch being landed into, then run this to collect

the worktree and merge-safety details:

WORKTREE_PATH=$(git rev-parse --show-toplevel) && \
BRANCH=$(git rev-parse --abbrev-ref HEAD) && \
git worktree list && \
TARGET_BRANCH="<target branch>" && \
TARGET_REPO=$(git worktree list | awk -v target="[$TARGET_BRANCH]" 'index($0, target) {print $1; exit}') && \
test -n "$TARGET_REPO" && \
UNCOMMITTED=$(git status --porcelain) && \
echo "WORKTREE_PATH=$WORKTREE_PATH" && \
echo "BRANCH=$BRANCH" && \
echo "TARGET_BRANCH=$TARGET_BRANCH" && \
echo "TARGET_REPO=$TARGET_REPO" && \
echo "UNCOMMITTED=$UNCOMMITTED" && \
if [ "$WORKTREE_PATH" = "$TARGET_REPO" ]; then echo "ERROR: You are in the target repo, not a worktree."; exit 1; fi && \
if [ -n "$UNCOMMITTED" ]; then echo "WARNING: Uncommitted changes exist"; fi && \
git log "$TARGET_BRANCH"..$BRANCH --oneline 2>/dev/null && \
echo "---FILES---" && \
git diff --name-only "$TARGET_BRANCH"..$BRANCH 2>/dev/null

If the target branch is unclear, ask the user. If you're in the target repo,

stop. If there are uncommitted changes, ask the user what to do.

2. Ensure work is merged into the target

git -C "$TARGET_REPO" merge-base --is-ancestor "$BRANCH" "$TARGET_BRANCH"

If this fails (exit code non-zero), run the /merge skill first. Wait for it to complete.

3. Tell the user, then tear down

Report what will happen: branch name and worktree path.

Then run the teardown as one bash command:

TARGET_REPO="<absolute path>" && \
WORKTREE_PATH="<absolute path>" && \
BRANCH="<branch name>" && \
cd "$TARGET_REPO" && \
git worktree remove "$WORKTREE_PATH" --force && \
git branch -d "$BRANCH" && \
echo "SESSION_CLOSED"

Substitute the actual values collected in step 1. Use -d (not -D) so git refuses if unmerged.

CRITICAL: This is the absolute last action. After running this command:

Rules

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.