Git Branch Cleanup
How do I delete all merged branches at once?
Run: `git branch --merged | grep -v '\*\|main\|master' | xargs -n 1 git branch -d`. This deletes all local branches that have been merged. The grep excludes your current branch and protected branches.
What about remote branches?
First prune: `git fetch --prune`. Then delete specific remote branches: `git push origin --delete branch-name`. For bulk deletion, you'll need a script that lists remote merged branches and deletes them.
How do I automate branch cleanup in CI?
Use GitHub Actions with a scheduled workflow. The `actions/stale` action can close old PRs, or write a custom script that deletes branches merged more than X days ago. Always exclude protected branches.
What if I accidentally delete a branch I needed?
Git doesn't immediately delete data. Use `git reflog` to find the commit hash, then `git checkout -b branch-name <hash>` to restore. This works for local branches; remote branches need backup or admin recovery.