Skip to content

Instantly share code, notes, and snippets.

@FBen3
Created April 22, 2025 14:00
Show Gist options
  • Save FBen3/06c50b630fbf7b01d6ea564fc1df1534 to your computer and use it in GitHub Desktop.
Save FBen3/06c50b630fbf7b01d6ea564fc1df1534 to your computer and use it in GitHub Desktop.
git-branch-cleanup
#!/bin/sh
set -euo pipefail
git fetch --prune
git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -D
git branch --merged | grep -vE '^\*|main|master|develop' | xargs git branch -d
@FBen3
Copy link
Author

FBen3 commented Apr 22, 2025

Can be aliased to a custom git command. This script cleans up you local branches:

  • updates & prunes remote references
  • force delete branches with no remotes
  • safe delete branches that have been merged into current branch (excluding main, master, develop, or the currently checked-out branch)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment