Skip to content

Instantly share code, notes, and snippets.

@wvfitzgerald
Last active January 13, 2026 22:51
Show Gist options
  • Select an option

  • Save wvfitzgerald/dd4986f662fba02df1c96d04eeb93b09 to your computer and use it in GitHub Desktop.

Select an option

Save wvfitzgerald/dd4986f662fba02df1c96d04eeb93b09 to your computer and use it in GitHub Desktop.
A bash script to safely delete remote Git branches that you created and have been fully merged into staging

Delete Merged Remote Branches

Distributed under GNU General Public License

A bash script to safely delete remote Git branches that you created and have been fully merged into staging. Sometimes it's just not practicle to have branches auto-delete when merged. For those occcasions I have written this simple script to cleanup the old branches that weren't deleted manually.

What It Does

This script finds and deletes remote branches that meet ALL of these criteria:

  • Created by you (based on git author/committer of the branch tip)
  • Fully merged into the staging branch
  • Not named main or staging

Safety Features

  • Dry-run preview: Shows which branches will be deleted before taking action
  • Confirmation prompt: Asks for explicit confirmation before deleting
  • Merge check: Only deletes branches that are fully merged - branches with additional unmerged commits are automatically excluded
  • Protected branches: Never deletes main or staging

Usage

  1. Make the script executable:
   chmod +x delete-merged-branches.sh
  1. Run the script from your local git repository:
   ./delete-merged-branches.sh
  1. Review the list of branches to be deleted

  2. Confirm with y to proceed or n to cancel

Requirements

  • Git installed and configured
  • Your user.name set in git config
  • Run from within a git repository
  • Network access to your remote repository

Example Output

Fetching latest remote branches...
Finding branches merged into staging created by: Your Name

The following branches will be deleted:
feature/login-fix
bugfix/header-alignment
feature/old-experiment

Do you want to proceed with deletion? (y/N):
#!/bin/bash
# Script to delete remote branches created by you that are merged into staging
# Excludes main and staging branches
set -e # Exit on error
STAGING_BRANCH="staging"
PROTECTED_BRANCHES=("main" "staging")
echo "Fetching latest remote branches..."
git fetch --prune
echo ""
echo "Finding branches merged into $STAGING_BRANCH created by: $(git config user.name)"
echo ""
# Get branches to delete
branches_to_delete=$(git for-each-ref --format='%(refname:short) %(authorname)' refs/remotes/origin --merged=$STAGING_BRANCH | \
grep "$(git config user.name)" | \
grep -v 'origin/staging' | \
grep -v 'origin/main' | \
awk '{print $1}' | \
sed 's/origin\///')
if [ -z "$branches_to_delete" ]; then
echo "No branches found to delete."
exit 0
fi
echo "The following branches will be deleted:"
echo "$branches_to_delete"
echo ""
read -p "Do you want to proceed with deletion? (y/N): " confirm
if [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]]; then
echo ""
echo "Deleting branches..."
echo "$branches_to_delete" | while read branch; do
if [ -n "$branch" ]; then
echo "Deleting: $branch"
git push origin --delete "$branch"
fi
done
echo ""
echo "Done!"
else
echo "Deletion cancelled."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment