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 :

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