Skip to content

Instantly share code, notes, and snippets.

@kadishmal
Created May 14, 2014 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kadishmal/62f9a8e5e43bb46cd13d to your computer and use it in GitHub Desktop.
Save kadishmal/62f9a8e5e43bb46cd13d to your computer and use it in GitHub Desktop.
A Bash script to delete already merged remote and local Git branches
#!/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