Git branch hygiene - delete merged/closed branch, prune stale refs, sync dependencies
Automates git branch hygiene after merging or closing PRs. Safely deletes merged branches (and force-deletes closed/abandoned branches), prunes stale remote tracking refs, syncs dependencies, and runs garbage collection.
Execute these steps in order:
git branch --show-current
Store the result as CURRENT_BRANCH.
If CURRENT_BRANCH is main or master:
git status --porcelain
If output is non-empty:
First check for merged PRs:
gh pr list --state merged --head CURRENT_BRANCH --json number
If merged, proceed to step 5.
If not merged, check for closed PRs:
gh pr list --state closed --head CURRENT_BRANCH --json number
If closed (result is non-empty):
If neither merged nor closed:
git branch --merged main | grep -w CURRENT_BRANCHgit branch -D manually if you want to force delete."Store whether the branch was MERGED or CLOSED for use in step 6.
git checkout main
If branch was MERGED:
git branch -d CURRENT_BRANCH
If branch was CLOSED (PR closed without merging):
git branch -D CURRENT_BRANCH
Report: "Deleted branch CURRENT_BRANCH." (mention if force-deleted due to closed PR)
git pull origin main
git fetch --prune
Find and delete local branches whose upstream is gone:
git for-each-ref --format '%(refname:short) %(upstream:track)' refs/heads | while read branch track; do
if [ "$track" = "[gone]" ]; then
git branch -d "$branch" 2>/dev/null && echo "Deleted stale branch: $branch"
fi
done
Report any branches deleted.
Detect package manager and sync:
uv.lock exists: uv syncbun.lockb exists: bun installpackage-lock.json exists: npm installrequirements.txt exists: pip install -r requirements.txtgit gc --prune=now
Report completion:
-D since branch is abandonedgit pull fails due to no remote, continue with local operationsgit branch --merged checkSearch for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer