Skip to content

Instantly share code, notes, and snippets.

@noahd1
Created July 12, 2024 18:10
Show Gist options
  • Save noahd1/9a1af51abc45bb4453e73e3165af595b to your computer and use it in GitHub Desktop.
Save noahd1/9a1af51abc45bb4453e73e3165af595b to your computer and use it in GitHub Desktop.
Script to prune local branches that have been merged
#!/bin/bash
# Fetch and prune to get the latest status of remote branches
git fetch --prune
# Iterate over each branch that has a gone status
for branch in $(git branch -vv | grep ': gone]' | awk '{print $1}'); do
# Check if the branch has been fully merged into the current branch
if git branch --merged | grep -w $branch > /dev/null; then
echo "Branch '$branch' is fully merged and can be safely deleted."
read -p "Do you want to delete the branch '$branch'? [y/N] " confirm
if [[ $confirm == [yY] ]]; then
git branch -d $branch
echo "Deleted branch '$branch'."
else
echo "Skipped deleting branch '$branch'."
fi
else
echo "Branch '$branch' has not been fully merged."
read -p "Do you want to force delete the branch '$branch'? [y/N] " confirm
if [[ $confirm == [yY] ]]; then
git branch -D $branch
echo "Force deleted branch '$branch'."
else
echo "Skipped deleting branch '$branch'."
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment