From empire-git
Closes a single Git worktree: identifies target, checks for dirty state, optionally pushes changes, removes worktree, and deletes branch per flags. Use to wrap up and clean one worktree.
npx claudepluginhub marcoskichel/empire --plugin empire-git[branch | path] [--push] [--discard] [--force]This skill is limited to using the following tools:
Finish work in a worktree: check for unsaved work, optionally push, remove the worktree, and let the user decide what happens to the branch.
Mandates invoking relevant skills via tools before any response in coding sessions. Covers access, priorities, and adaptations for Claude Code, Copilot CLI, Gemini CLI.
Share bugs, ideas, or general feedback.
Finish work in a worktree: check for unsaved work, optionally push, remove the worktree, and let the user decide what happens to the branch.
User input: $ARGUMENTS
Flag semantics (do not conflate):
| Flag | Meaning |
|---|---|
--push | Push the branch to origin before removing the worktree. |
--discard | Skip the dirty-check prompt and proceed even with uncommitted changes. |
--force | Skip the cleanup-option prompt; default to "remove worktree + delete branch". |
--discard and --force are independent. The user can pass either, both, or neither. Never treat one as implying the other.
Priority order:
$ARGUMENTS contains a branch name or worktree path, use that.git worktree list and ask the user to pick one.Parse git worktree list --porcelain to find the worktree path and branch. Identify the main working tree (the first entry) — it cannot be closed.
git -C "<worktree-path>" status --porcelain
If clean (empty output): proceed to Step 3.
If dirty (uncommitted changes):
Show the user what's pending:
git -C "<worktree-path>" status --short
Then present options:
/commit in the worktree, then re-run /empire-git:worktree-closeIf --discard was in $ARGUMENTS, skip this prompt and proceed with force removal of uncommitted work.
If --push was in $ARGUMENTS, or the user mentioned pushing:
git -C "<worktree-path>" push -u origin "<branch>"
If push fails, report the error and stop. If no --push flag, skip this step.
If there are unpushed commits and the user did NOT request --push, mention it:
This branch has X unpushed commit(s) that are not backed up to the remote. Add
--pushto push before closing, or continue to close without pushing.
Ask the user what they'd like to do. Present these choices:
git branch -d (safe delete — refuses if unmerged).Pick a sensible default based on context:
If --force was specified, skip the cleanup-option prompt and use option 2 (but still use safe delete with git branch -d).
git worktree remove "<worktree-path>"
If the worktree is dirty and the user confirmed discard (or passed --discard):
git worktree remove --force "<worktree-path>"
Critical: both commands MUST run in a single Bash call. Claude Code's Bash tool resolves the shell's cwd at the start of each invocation. If the session is running from inside the worktree and you remove the worktree in one Bash call, the next Bash call will fail immediately ("No such file or directory") before any command — including git -C — can execute. The branch delete becomes impossible and the branch is orphaned.
The worktree must be removed before the branch delete (git branch -d refuses to delete a branch that's checked out in a worktree). So the correct sequence is: remove worktree, then delete branch — but in one shell invocation.
First, resolve the main working tree path:
MAIN_REPO=$(git worktree list --porcelain | awk '/^worktree / { print $2; exit }')
Then run both commands in a single Bash call, chained with &&:
git -C "$MAIN_REPO" worktree remove "<worktree-path>" && git -C "$MAIN_REPO" branch -d "<branch>"
Why this works: the shell resolves its cwd once when the Bash call starts. Both git commands use -C "$MAIN_REPO" so git operates from the main repo regardless of the shell's cwd. Since they run in the same shell process, the cwd is only checked once — at launch — before any directory is removed.
If git branch -d fails (branch not fully merged), tell the user:
Branch '' has unmerged commits. Keeping the branch. To force-delete:
git branch -D <branch>
Do nothing. Confirm to the user that the worktree is still active.
git worktree prune
Print a summary:
Worktree closed.
Path: <worktree-path> (removed)
Branch: <branch> (deleted | kept | pushed)
The main working tree is not a worktree. It's the user's primary repo checkout — removing it would be catastrophic. If someone accidentally targets it, explain the difference.
Uncommitted work is sacred. Silently discarding changes is one of the worst things a tool can do. The user should always see what's at risk and explicitly choose to discard. The --discard flag exists for when they've already made that choice. --force only skips the cleanup-option prompt — it does NOT authorize discarding uncommitted work.
Use git branch -d (safe delete), not -D. If -d refuses, it means the branch has unmerged commits — that's valuable information to surface, not override. Tell the user the command to force-delete if they really want to.
Use git worktree remove, not rm -rf. Git tracks worktree metadata internally; removing the directory without telling git leaves stale references that cause confusing errors later. If anything goes wrong, git worktree prune cleans up the metadata.