Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Git Worktree Remove?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/florianbruniaux-git-worktree-remove)
---
name: git-worktree-remove
description: Safely remove a git worktree with branch cleanup and safety checks
argument-hint: "<worktree-name-or-path> [--force] [--keep-branch] [--keep-remote]"
effort: low
when_to_use: "Use when removing a specific worktree by name."
disable-model-invocation: true
---
# Git worktree remove
Safely remove a single git worktree with branch cleanup, merge verification, and database branch teardown.
**Core principle:** Safety checks first, then clean removal of worktree + branch + DB resources.
**Part of:** Worktree Lifecycle Suite: [`/git-worktree`](../git-worktree/SKILL.md) | [`/git-worktree-status`](../git-worktree-status/SKILL.md) | [`/git-worktree-clean`](../git-worktree-clean/SKILL.md)
## Process
1. **Validate Target**: Identify worktree to remove
2. **Safety Check**: Protect main/develop branches
3. **Check Merge Status**: Warn if branch has unmerged changes
4. **Check Uncommitted Changes**: Warn if worktree has dirty state
5. **Remove Worktree**: `git worktree remove`
6. **Delete Local Branch**: `git branch -d` (or `-D` with confirmation)
7. **Delete Remote Branch**: `git push origin --delete` (with confirmation)
8. **Database Cleanup Reminder**: Suggest DB branch deletion if applicable
9. **Prune References**: `git worktree prune`
## Safety checks
Shell variables do not persist between separate Bash calls. Run the blocks in one call, or set the variables again at the top of each call. An empty or unknown value must never lead to a deletion.
### Resolve the target
```bash
TARGET="<worktree name or path from the arguments>"
for DIR in ".worktrees/$TARGET" "worktrees/$TARGET" "$TARGET"; do
[ -d "$DIR" ] && break
done
WORKTREE_PATH=$(cd "$DIR" 2>/dev/null && pwd -P)
if [ -z "$WORKTREE_PATH" ] || [ "$(git -C "$WORKTREE_PATH" rev-parse --show-toplevel 2>/dev/null)" != "$WORKTREE_PATH" ]; then
echo "BLOCKED: '$TARGET' is not a worktree"
exit 1
fi
BRANCH=$(git -C "$WORKTREE_PATH" symbolic-ref --quiet --short HEAD) || {
echo "BLOCKED: detached HEAD, removing it can orphan commits. Ask the user how to keep them."
exit 1
}
```
### Protected branches
```bash
# Never remove worktrees for these branches (configurable)
PROTECTED_BRANCHES="main master develop staging production"
[ -n "$BRANCH" ] || { echo "BLOCKED: branch name unknown"; exit 1; }
case " $PROTECTED_BRANCHES " in
*" $BRANCH "*)
echo "BLOCKED: Cannot remove worktree for protected branch '$BRANCH'"
echo "Protected branches: $PROTECTED_BRANCHES"
exit 1 ;;
esac
```
### Uncommitted changes
```bash
# git -C keeps the shell outside the directory that is about to be removed
: "${WORKTREE_PATH:?}"
if [ -n "$(git -C "$WORKTREE_PATH" status --porcelain)" ] && [ "$FORCE" != true ]; then
echo "STOP: Worktree has uncommitted changes:"
git -C "$WORKTREE_PATH" status --short
echo ""
echo "Options:"
echo " 1. Commit changes first"
echo " 2. Force remove (--force)"
echo " 3. Cancel"
exit 1
fi
```
On this stop, show the options to the user and wait for the answer. Set `FORCE=true` only if the user passed `--force` or chose option 2.
### Merge status
```bash
# Check if branch is merged into main
MAIN_BRANCH=$(git symbolic-ref --quiet refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
if [ -z "$MAIN_BRANCH" ]; then
for CANDIDATE in main master; do
git show-ref --verify --quiet "refs/heads/$CANDIDATE" && MAIN_BRANCH=$CANDIDATE && break
done
fi
[ -n "$MAIN_BRANCH" ] || { echo "BLOCKED: main branch unknown, ask the user for it"; exit 1; }
if git merge-base --is-ancestor "${BRANCH:?}" "$MAIN_BRANCH" 2>/dev/null; then
echo "Branch '$BRANCH' is merged into $MAIN_BRANCH. Safe to delete."
MERGED=true
else
echo "WARNING: Branch '$BRANCH' is NOT merged into $MAIN_BRANCH."
echo "You may lose work if you delete this branch."
MERGED=false
fi
```
## Removal steps
Set `FORCE`, `KEEP_BRANCH` and `KEEP_REMOTE` to `true` or `false` from the flags.
```bash
# 1. Remove the worktree (stop here if it fails)
if [ "$FORCE" = true ]; then
git worktree remove --force "${WORKTREE_PATH:?}" || exit 1
else
git worktree remove "${WORKTREE_PATH:?}" || exit 1
fi
# 2. Delete local branch, only if merged. An unset value deletes nothing.
if [ "$KEEP_BRANCH" = false ] && [ "$MERGED" = true ]; then
git branch -d "${BRANCH:?}"
else
echo "Local branch '$BRANCH' kept"
fi
# 3. Prune stale references
git worktree prune
```
### Delete an unmerged local branch
Skip with `--keep-branch`. **STOP.** If the branch is not merged, show `git log --oneline "$MAIN_BRANCH..$BRANCH"` and ask the user whether to delete it. `--force` does not answer this question. Run the next block only after an explicit yes in the conversation.
```bash
git branch -D "${BRANCH:?}"
```
### Delete the remote branch
Skip with `--keep-branch` or `--keep-remote`.
```bash
if git ls-remote --exit-code --heads origin "refs/heads/${BRANCH:?}" >/dev/null; then
git fetch origin "$BRANCH" && git log --oneline "${MAIN_BRANCH:?}..FETCH_HEAD"
echo "Remote branch 'origin/$BRANCH' exists"
else
echo "No remote branch found (or origin unreachable): delete nothing"
fi
```
**STOP.** If the remote branch exists, show the output above (commits on the remote that are not in the main branch) and ask the user whether to delete it. Run the next block only after an explicit yes in the conversation. A merged local branch is not an approval.
```bash
git push origin --delete "${BRANCH:?}"
```
## Database branch cleanup
**After worktree removal, remind about associated database branches:**
```bash
# Detect database provider (same logic as /git-worktree)
if [ -f ".env" ] && grep -q "neon" ".env"; then
echo ""
echo "DB Cleanup: neonctl branches delete $BRANCH_SLUG"
elif [ -f ".pscale.yml" ]; then
echo ""
DB_NAME=$(grep 'database:' .pscale.yml | awk '{print \$2}')
echo "DB Cleanup: pscale branch delete $DB_NAME $BRANCH_SLUG"
elif [ -f ".env" ] && grep -q "postgresql" ".env"; then
echo ""
echo "DB Cleanup: psql \$DATABASE_URL -c \"DROP SCHEMA ${BRANCH_SLUG} CASCADE;\""
fi
```
## Report format
**Successful removal (merged branch):**
```
Removed worktree: .worktrees/feat/auth
Worktree directory: deleted
Local branch feat/auth: deleted (was merged)
Remote branch origin/feat/auth: deleted
References: pruned
DB reminder: neonctl branches delete feat-auth
```
**Removal with warnings (unmerged branch):**
```
Removed worktree: .worktrees/feat/experimental
Worktree directory: deleted
Local branch feat/experimental: deleted (was NOT merged - forced)
Remote branch: no remote branch found
References: pruned
WARNING: Branch was not merged. Changes may be lost.
Last commit: a1b2c3d "WIP: experimental auth flow"
```
## Flags
| Flag | Effect |
|------|--------|
| `--force` | Skip the uncommitted changes stop (does not approve any branch deletion) |
| `--keep-branch` | Remove worktree but keep the branch |
| `--keep-remote` | Don't delete remote branch |
## Quick reference
| Situation | Action |
|-----------|--------|
| Branch is merged | Safe delete (branch -d) |
| Branch is unmerged | Warn + require confirmation (branch -D) |
| Uncommitted changes | Warn + offer force/cancel |
| Protected branch (main/develop) | Block removal |
| Remote branch exists | Ask to delete remote |
| DB branch detected | Remind with exact command |
| Stale references | Auto-prune |
## Common mistakes
**Removing worktree for main/develop**
- Always blocked by safety check. Reconfigure protected branches if needed.
**Deleting unmerged branch without checking**
- Always verify merge status. Deleting an unmerged branch requires explicit user approval.
**Forgetting database branch cleanup**
- Leaves orphaned DB branches consuming resources. Command reminds automatically.
**Using `rm -rf` instead of `git worktree remove`**
- Leaves stale worktree references in `.git/worktrees/`. Always use git commands.
## Usage
```
/git-worktree-remove feat/auth
/git-worktree-remove fix/login-bug --force
/git-worktree-remove refactor/db --keep-branch
```
Branch or worktree path: $ARGUMENTS