Skip to content

Instantly share code, notes, and snippets.

@lukeplausin
Last active November 16, 2022 11:48
Show Gist options
  • Save lukeplausin/06c102792a2dd42ad6727cf34faa99e7 to your computer and use it in GitHub Desktop.
Save lukeplausin/06c102792a2dd42ad6727cf34faa99e7 to your computer and use it in GitHub Desktop.
Clean up merged git branches
#!/bin/zsh
# Do merged git branches clutter up your local machine?
# This handy script can automatically clean them up for you.
# To install - put the script somewhere on the filesystem and add a source command to your .bashrc / .zshrc
# e.g. - 'source ~/code/scrapbook/cleanup_git.sh'
# When the script is sourced you will get two commands -
# "gitclean" - delete merged branches in current folder (with prompt)
# "gitcleanall" - iterate all my project directories running "gitclean" in each of them
# To change your project dir, edit this line - "export PROJECT_DIR=$HOME/code"
# This script is written for zsh (i.e. macOS), for bash/linux you might need to change the read command
# on line 29 to 'read -p "These branches will be deleted locally - are you sure? " reply'
export PROJECT_DIR=$HOME/code
function gitclean {
if [ -d ".git" ]; then
echo "Merged branches:" ;
to_delete=$(git branch --merged| egrep -v "(^\*|master|main|dev)") ;
if [ -z "$to_delete" ]; then
echo "No merged branches." ;
else
echo $to_delete
read "reply?These branches will be deleted locally - are you sure? "
echo # (optional) move to a new line
if [[ ! $reply =~ ^[Yy]$ ]]
then
echo "Exit."
else
echo $to_delete | xargs git branch -d
echo "Deleted!" ;
fi ;
fi ;
else
echo "No git directory." ;
fi
}
function gitcleanall {
pushd $PROJECT_DIR
for dirname in $(ls -d */); do
echo "" ;
pushd $dirname ;
echo "Running gitclean" ;
gitclean ;
popd ;
done
popd ;
}
superlint () {
# Use the Github super-linter in the current directory!
sedpwd=$(echo `pwd` | sed 's/\//\\\//g');
docker run \
-e RUN_LOCAL=true \
-e USE_FIND_ALGORITHM=true \
-v `pwd`:/tmp/lint github/super-linter:latest \
2>&1 | sed "s/\/tmp\/lint/$sedpwd/g";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment