Skip to content

Instantly share code, notes, and snippets.

@JoshuaSkootsky
Created July 11, 2024 15:53
Show Gist options
  • Save JoshuaSkootsky/e375f487cb602c0bcbd134a4da2a4b18 to your computer and use it in GitHub Desktop.
Save JoshuaSkootsky/e375f487cb602c0bcbd134a4da2a4b18 to your computer and use it in GitHub Desktop.
Prune Remote and Local Branches
#!/bin/bash
# Fetch the latest from the remote
git fetch origin
# Get the current branch
current_branch=$(git rev-parse --abbrev-ref HEAD)
echo "Current branch: $current_branch"
# Check if the current branch exists on the remote
if ! git ls-remote --exit-code --heads origin $current_branch > /dev/null 2>&1; then
echo "Warning: The current branch '$current_branch' doesn't exist on the remote."
echo "You may need to push this branch first or switch to a branch that exists remotely."
exit 1
fi
# List of branches to never delete
protected_branches="main master develop staging production"
# Function to filter out protected branches and special refs
filter_branches() {
while read -r branch; do
# Remove leading whitespace and "origin/" prefix
branch_name=$(echo $branch | sed 's/^\s*origin\///' | sed 's/^[[:space:]]*//')
# Skip HEAD ref and protected branches
if [[ "$branch_name" != "HEAD "* ]] && ! echo $protected_branches | grep -w -q $branch_name; then
echo "$branch_name"
fi
done
}
# List merged remote branches
echo "Fetching merged remote branches..."
merged_remote_branches=$(git branch -r --merged origin/$current_branch 2>/dev/null | grep -v "^\s*origin/$current_branch$" | filter_branches)
# List merged local branches
echo "Fetching merged local branches..."
merged_local_branches=$(git branch --merged origin/$current_branch | grep -v "^\*" | grep -v "$current_branch" | filter_branches)
if [ -z "$merged_remote_branches" ] && [ -z "$merged_local_branches" ]; then
echo "No merged branches found."
exit 0
fi
echo "Merged remote branches (excluding protected branches and HEAD):"
echo "$merged_remote_branches"
echo -e "\nMerged local branches (excluding protected branches):"
echo "$merged_local_branches"
# Ask for confirmation
read -p "Do you want to delete these branches? (y/n): " confirm
if [ "$confirm" != "y" ]; then
echo "Operation cancelled."
exit 0
fi
# Delete merged remote branches
if [ -n "$merged_remote_branches" ]; then
echo "Deleting remote branches..."
echo "$merged_remote_branches" | while read branch_name; do
if [ -n "$branch_name" ]; then
echo "Deleting remote branch: $branch_name"
git push origin --delete "$branch_name"
fi
done
else
echo "No remote branches to delete."
fi
# Delete merged local branches
if [ -n "$merged_local_branches" ]; then
echo -e "\nDeleting local branches..."
echo "$merged_local_branches" | while read branch_name; do
if [ -n "$branch_name" ]; then
echo "Deleting local branch: $branch_name"
git branch -d "$branch_name"
fi
done
else
echo "No local branches to delete."
fi
echo "Cleanup complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment