Skip to content

Instantly share code, notes, and snippets.

@n8jadams
Created February 3, 2021 17:03
Show Gist options
  • Save n8jadams/dd31c0fe1892a28a8ba0647151fd0568 to your computer and use it in GitHub Desktop.
Save n8jadams/dd31c0fe1892a28a8ba0647151fd0568 to your computer and use it in GitHub Desktop.
A nice git function to clean up your local branches. Drop in your bashrc file(s).
# Deletes local branches that have been deleted on the remote
function gpl() {
# Determine the name of the trunk
trunkname=$(git symbolic-ref --short refs/remotes/origin/HEAD | sed 's@^origin/@@')
# List branches that have been removed from origin and write to file
git branch --merged $(git rev-parse $trunkname) | awk '{$1=$1};1' | egrep -v "(^\*|^\s*($trunkname)$)" > /tmp/branchesToPurge
# Open the file for editing
vim /tmp/branchesToPurge
# Print list of branches to be deleted
echo "The following branches will be deleted:"
cat /tmp/branchesToPurge
# Ask if we should proceed
read -p "Are you sure you want to delete these branches (y/n)? " answer
case ${answer:0:1} in
y | Y)
echo "Deleting..."
# Delete branches listed in file
xargs git branch -D </tmp/branchesToPurge
;;
*)
echo "Aborted"
;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment