Cleanup old git branches
December 20, 2018 —
Cédric Dekimpe
Yesterday I had to clean some very old branches on a project’s codebase. Here’s a few git tricks I had to use.
List merged branch (excluding master and staging) :/
git branch --merged | egrep -v "(^\*|master|staging)"
Delete them locally :
git branch --merged | egrep -v "(^\*|master|staging)" | xargs -n 1 git branch -d
Delete them remotely :
git branch -r --merged | grep -v "origin/master$" | grep -v "origin/staging$" | sed 's/\s*origin\///' | xargs -n 1 git push --delete origin git remote prune origin
Source : https://stackoverflow.com/questions/6127328/how-can-i-delete-all-git-branches-which-have-been-merged
Display branches with oldest commit datetime :
for branch in `git branch -r | grep -v HEAD`;do echo -e `git show --format="%ci %cr" $branch | head -n 1` \\t$branch; done | sort -r