Automate Git Branch Hygiene with the /clean-up skill for Claude Code
If you use Claude Code, you've probably accumulated stale branches from merged PRs, deleted remotes, and work that drifted behind main weeks ago. The most annoying part is when you get merge conflicts in github from your own work. I had been dealing with this all manually but finally decided to built a /clean-up skill that handles it all in one command.
What It Does
Type /clean-up and Claude runs a full repository hygiene pass:
Compacts the conversation to free up context space
Fetches from origin with --prune
Deletes local branches whose PRs are merged (using gh pr list --state merged)
Deletes branches whose remote tracking branch is gone
Rebases remaining branches onto origin/main, resolving simple conflicts
Returns to your starting branch and prints a summary
It won't delete main, won't force-push to main, and aborts rebases rather than making bad merge decisions.
How to Create It
Skills are markdown files in ~/.claude/skills/. Each skill gets a directory with a SKILL.md file containing YAML frontmatter (name + trigger description) and markdown instructions.
mkdir -p ~/.claude/skills/clean-upThen create ~/.claude/skills/clean-up/SKILL.md. The file has two parts:
Frontmatter tells Claude when to activate the skill:
---
name: clean-up
description: >-
This skill should be used when the user asks to "clean up",
"tidy up branches", "prune branches", "rebase all branches",
"sync branches", or says "/clean-up".
---Body contains the step-by-step workflow in plain markdown. You write it the same way you'd write a runbook for a teammate: describe each step, include the shell commands, and note edge cases. For the full body, see # (or the appendix below).
The key parts of the workflow body:
- Use gh pr list --head <branch> --state merged to detect merged branches (more reliable than git branch --merged, which misses squash merges)
- Use git branch -vv | grep ': gone]' to find orphaned branches
- Attempt conflict resolution during rebase but abort if ambiguous
- Present results in a summary table at the endThings You Might Customize
Customize this skill for your own flows and whatever details you need handled.
Protected branches: Change main to master or develop if needed
Force push safety: Swap git push -f for git push --force-with-lease
Sapling users: Substitute sl pull, sl rebase -d main, etc.
The Broader Pattern
Skills work well for any multi-step workflow you'd otherwise do manually and probably forget a step of: release prep, feature scaffolding, PR checklists. The skill file is just a prompt. If you can describe the workflow in plain English with shell commands, you can make it a skill.
Comments
Leave a Comment
Sign in with your Microsoft account to leave a comment.
Sign in with Microsoft