Today, I cleaned hundreds of stale branches with (🚧 use with caution 🚧):
git for-each-ref refs/remotes/origin \
--exclude='refs/remotes/origin/release/*' \
--format='%(committerdate:unix) %(refname:short)' |
awk -v cutoff="$(gdate -d '6 months ago' +%s)" '
$1 < cutoff {
sub("^origin/", "", $2)
print $2
}
' |
xargs -r -n 1 echo git push --delete origin
Things to note from the command above:
- Use
--excludeto avoid deleting certain branches. - The
--formatuses a Unix timestamp soawkcan compare dates numerically. - Notice the use of
gdatefrom the GNU Core Utilities.- On macOS, install with
brew install coreutils. - Set the date offset to control how old a branch must be before deletion.
- On macOS, install with
- The
echoturns the command into a dry run so you can verify the output before deleting anything.- Remove
echoif you want to delete directly.
- Remove
If you just want to inspect branches, run:
git for-each-ref refs/remotes/origin \
--sort=committerdate \
--exclude='refs/remotes/origin/release/*' \
--format='%(committerdate:short) %(refname:short)'