Skip to content

Instantly share code, notes, and snippets.

@kahunacohen
Last active August 15, 2021 14:53
Show Gist options
  • Save kahunacohen/5630103c9159d4bcff5013d853ca9980 to your computer and use it in GitHub Desktop.
Save kahunacohen/5630103c9159d4bcff5013d853ca9980 to your computer and use it in GitHub Desktop.
A collection of useful git bash functions
#!/usr/bin/env bash
# A collection of useful functions related to git. This file must
# be sourced to bring its functions into the user path.
# delete_branches is a function, that once sourced, attempts to delete local
# and remote git branches from stdin or a file. The script
# prompts the user to delete each branch (local and remote).
#
# Example:
# $ git branch -r --sort=-committerdate | delete_branches
#
# To dos:
# * Check first if the same local branch exists, if not don't try to delete it.
# * Remember if you chose to skip deleting branch by creating a file and don't
# ask the next time.
delete_branches() {
git config --global pager.branch false
git remote prune origin
while read line
do
branch=$(echo $line | sed -e 's:origin/::g')
echo "Do you want to delete the branch, '$branch'?"
select yn in "Yes" "No"; do
case $yn in
Yes ) git branch -D $line || git push origin :$branch; break;;
No ) echo skipping...; break;;
esac
done
done < "${1:-/dev/stdin}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment