john-science / john-science.github.io

A Blog of Minor Obsessions
http://john-science.github.io/
Apache License 2.0
3 stars 4 forks source link

Another Git Cleaning Trick #97

Closed john-science closed 5 years ago

john-science commented 5 years ago

Branch Cleaning

May I recommend a little Spring Cleaning?

git fetch --all;git fetch --prune;for branch in `git branch -r | grep -v HEAD`;do echo -e `git show --format="%cd, %an," --date=format:'%Y-%m-%d' $branch | head -n 1` \\t$branch; done | sort -r | sed 's/origin\///g' | sed 's/\t//g' | sort
john-science commented 5 years ago

Here's my first stab at the script:

#!/bin/bash

#############################################################
#  Find the loneliest branches in your Git repo.            #
#                                                           #
#  i.e. Sort all the branches in a local Git repo by the    #
#       date of their last commit, and show the commiter.   #
#                                                           #
#  WARNING: This script will be slow for large repos.       #
#############################################################

# set file paths
OUT='lonely_branches.txt'

# bring your local repo up to date
git fetch --all

# prune all the origin-deleted branches locally
git fetch --prune

# find all branches and sort them by age and commiter
for branch in `git branch -r | grep -v HEAD`;do echo -e `git show --format="%cd, %an," --date=format:'%Y-%m-%d' $branch | head -n 1` \\t$branch; done |    sort -r | sed 's/origin\///g' | sed 's/\t//g' | sort -u > ${OUT}