remove remote branches identical to master - chunhualiao/public-docs GitHub Wiki
#!/bin/bash
# This script detects and removes remote branches that are identical to the remote master branch.
# Fetch latest remote updates
git fetch origin
# Get the commit hash of remote master
master_commit=$(git rev-parse origin/master)
# List all remote branches except master
remote_branches=$(git branch -r | grep -v 'origin/master' | grep 'origin/' | sed 's/origin\///')
for branch in $remote_branches; do
# Get the commit hash of the remote branch
branch_commit=$(git rev-parse origin/$branch)
# Check if the commits are the same
if [ "$branch_commit" = "$master_commit" ]; then
echo "Branch '$branch' is identical to 'origin/master'."
read -p "Do you want to delete this remote branch? (y/N): " confirm
if [ $confirm == [yY] ](/chunhualiao/public-docs/wiki/-$confirm-==-[yY]-); then
git push origin --delete $branch
echo "Deleted remote branch '$branch'."
else
echo "Skipped deleting remote branch '$branch'."
fi
else
# If commits differ, check if the content is identical by diffing the two branches
diff_output=$(git diff origin/master..origin/$branch)
if [ -z "$diff_output" ]; then
echo "Branch '$branch' has identical content to 'origin/master'."
read -p "Do you want to delete this remote branch? (y/N): " confirm
if [ $confirm == [yY] ](/chunhualiao/public-docs/wiki/-$confirm-==-[yY]-); then
git push origin --delete $branch
echo "Deleted remote branch '$branch'."
else
echo "Skipped deleting remote branch '$branch'."
fi
else
echo "Branch '$branch' differs from 'origin/master'. Keeping branch."
fi
fi
done