Skip to content

Instantly share code, notes, and snippets.

@ufuk
Last active October 31, 2017 08:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ufuk/6fda2b5c6c3b43c0f2d7b56aba686b59 to your computer and use it in GitHub Desktop.
Save ufuk/6fda2b5c6c3b43c0f2d7b56aba686b59 to your computer and use it in GitHub Desktop.
Lists git branches ordered by last change time to find out which ones are stale branches.
#!/bin/bash
# DESCRIPTION
# -----------
#
# Produces CSV output like this:
#
# LAST CHANGE TIME, TIME ELAPSED, AUTHOR, BRANCH
# 2017-01-16 14:56:26 +0000, 3 months ago, john.doe@example.com, origin/bug-fix-1
# 2017-01-23 18:27:53 +0300, 2 months ago, john.doe@example.com, origin/new-feature-1
# 2017-01-31 19:25:59 +0300, 2 months ago, john.doe@example.com, origin/new-feature-1
# 2017-02-06 19:26:20 +0300, 4 weeks ago, john.doe@example.com, origin/new-feature-1
# 2017-02-20 19:51:03 +0300, 2 weeks ago, john.doe@example.com, origin/bug-fix-2
# 2017-03-03 10:53:57 +0300, 5 days ago, john.doe@example.com, origin/master
# ...
#
# You can grep "months ago" including lines. (or whatever you wish)
# Then replace using this regex ".+origin/" with "git push origin --delete ".
# Finally you will have a command list that deletes stale branches.
#
# For example, in a git repository's root run this command:
#
# bash ../find-out-stale-branches.sh | grep "months ago" | perl -pe "s/.*origin\//git push origin --delete /"
#
# Then it'll produce something like this:
#
# git push origin --delete bug-fix-1
# git push origin --delete new-feature-1
# git push origin --delete ...
# Print header
echo
echo "LAST CHANGE TIME, TIME ELAPSED, AUTHOR, BRANCH";
# Prepare report
for branch in `git branch -r | grep -v HEAD`; do
echo -e `git show --format="%ci,%cr,%ae" $branch | head -n 1`,$branch;
done | sort | sed 's/,/, /g'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment