Git Worktree Cleanup
Remove worktrees whose feature branches have been merged into develop or whose PRs are closed/merged.
Quick cleanup (merged branches)
Run the cleanup script from the repository root:
bash
bash scripts/worktree/cleanup-worktrees.sh
This script will:
- •Fetch latest from remote
- •Find all worktrees with branches merged into
origin/develop - •Remove those worktrees and delete the local branches
- •Run
git worktree pruneto clean up stale references - •Report what was removed
Targeted cleanup (single worktree)
To remove a specific worktree manually:
bash
git worktree remove ../worktrees/feature-AB-<ticket>-<short-desc> git branch -d feature/AB-<ticket>-<short-desc>
Enhanced cleanup with PR status check
If the cleanup script misses worktrees (e.g. squash-merged PRs where the branch doesn't show as merged in git), use GitHub MCP to verify:
- •Run
git worktree listto get all active worktrees. - •For each worktree branch, use user-github MCP
list_pull_requests(state: closed, head: branch name) to check if a PR was merged. - •If the PR state is
merged, remove the worktree and branch:bashgit worktree remove <worktree-path> git branch -D <branch-name>
- •Run
git worktree pruneat the end.
Important notes
- •The script only removes worktrees under
../worktrees/— it never touches the main working directory. - •Only branches matching
feature/*,bugfix/*,qol/*, orfix/*are considered for cleanup. - •If a branch has uncommitted changes,
git worktree removewill fail safely — no data loss. - •Use
git branch -D(force) for squash-merged branches since git won't recognize them as merged.