Created
May 14, 2014 05:29
-
-
Save kadishmal/62f9a8e5e43bb46cd13d to your computer and use it in GitHub Desktop.
A Bash script to delete already merged remote and local Git branches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Usage: | |
# chmod +x delete_merged_git_branches.sh | |
# ./delete_merged_git_branches.sh /path/to/git/repo | |
DIR="$1" | |
if [ -z "$DIR" ]; then | |
echo "Error: Git directory path should be provided." | |
echo "Usage:" | |
echo " ./delete_merged_git_branches.sh /path/to/git/repo" | |
else | |
cd "$DIR" | |
for OUTPUT in $(git branch -a --merged) | |
do | |
if [[ $OUTPUT =~ ^remotes\/origin\/ ]]; then | |
[[ $OUTPUT =~ ^remotes\/origin\/(.+)$ ]] | |
BRANCH="${BASH_REMATCH[1]}" | |
if [ $BRANCH = "HEAD" ] || [ $BRANCH = "master" ]; then | |
echo "Skipping $BRANCH branch deletion." | |
else | |
printf "Deleting $BRANCH branch...\r" | |
git push origin :$BRANCH | |
git branch -d $BRANCH | |
fi | |
fi | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment