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.
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
stagingbranch - Not named
mainorstaging
- 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
mainorstaging
- Make the script executable:
chmod +x delete-merged-branches.sh- Run the script from your local git repository:
./delete-merged-branches.sh-
Review the list of branches to be deleted
-
Confirm with
yto proceed ornto cancel
- Git installed and configured
- Your
user.nameset in git config - Run from within a git repository
- Network access to your remote repository
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):