Skip to content

Instantly share code, notes, and snippets.

@arodland
Last active August 29, 2015 14:13
Show Gist options
  • Save arodland/9514b9e0767cec55b12b to your computer and use it in GitHub Desktop.
Save arodland/9514b9e0767cec55b12b to your computer and use it in GitHub Desktop.
# make sure we're up to date to begin with
git remote update --prune
echo Archiving branches that have been merged into master for 2 weeks...
MASTER2WEEKS=$(git rev-list -n 1 --before='2 weeks ago' origin/master)
OLDBRANCHES=$(git branch -r --merged "$MASTER2WEEKS" | sed 's/\s*origin\///')
echo "$OLDBRANCHES"
echo "$OLDBRANCHES" | /usr/bin/perl -lpe 's/(.*)/+refs\/remotes\/origin\/$1:refs\/archive\/$1 :refs\/heads\/$1/' | xargs --verbose -n 50 --no-run-if-empty git push origin
git remote update --prune
echo Archiving branches that haven\'t been touched in 90 days...
OLDBRANCHES=$(git for-each-ref --sort=committerdate refs/remotes/origin --format='%(refname) %(committerdate:short)' | perl -MDateTime -lane 'BEGIN { $cutoff = DateTime->now->subtract(days => 90)->ymd; } print $F[0] if $F[1] lt $cutoff' | sed -e 's/refs\/remotes\/origin\///')
echo "$OLDBRANCHES"
echo "$OLDBRANCHES" | perl -lpe 's/(.*)/+refs\/remotes\/origin\/$1:refs\/archive\/$1 :refs\/heads\/$1/' | xargs --verbose -n 50 --no-run-if-empty git push origin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment