Delete Git Branches That Have Been Merged

Got a lot of old git branches hanging around? Here’s a little script
that will delete the branches that have been marged.

It’ll print out the branches to be deleted, and then prompt for whether
you want to delete them.

If your top-level branch i…


This content originally appeared on Dave Ceddia and was authored by Dave Ceddia

Got a lot of old git branches hanging around? Here’s a little script that will delete the branches that have been marged.

It’ll print out the branches to be deleted, and then prompt for whether you want to delete them.

If your top-level branch isn’t called “main”, customize the MAIN variable to match.

Script: Delete old git branches

#!/bin/bash

# Change this to match the name of your top level branch
MAIN=main

echo "These branches have been merged into $MAIN and will be deleted:"
echo
git branch --merged $MAIN | grep -v "^\* $MAIN"
echo

read -p "Continue? [y/N] " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
  exit 1
fi

git branch --merged $MAIN | grep -v "^\* $MAIN" | xargs -n 1 -r git branch -d

Copy/paste this into a file in your repo or elsewhere (like git-cleanup.sh) and make it executable with chmod +x git-cleanup.sh

Bonus: if you want this file to be ignored, but you don’t want to clutter up the shared .gitignore file with your own local scripts, you can edit .git/info/exclude and list this file there. That file works as a local gitignore.

Delete Git Branches That Have Been Merged was originally published by Dave Ceddia at Dave Ceddia on August 04, 2022.


This content originally appeared on Dave Ceddia and was authored by Dave Ceddia


Print Share Comment Cite Upload Translate Updates
APA

Dave Ceddia | Sciencx (2022-08-04T15:05:07+00:00) Delete Git Branches That Have Been Merged. Retrieved from https://www.scien.cx/2022/08/04/delete-git-branches-that-have-been-merged/

MLA
" » Delete Git Branches That Have Been Merged." Dave Ceddia | Sciencx - Thursday August 4, 2022, https://www.scien.cx/2022/08/04/delete-git-branches-that-have-been-merged/
HARVARD
Dave Ceddia | Sciencx Thursday August 4, 2022 » Delete Git Branches That Have Been Merged., viewed ,<https://www.scien.cx/2022/08/04/delete-git-branches-that-have-been-merged/>
VANCOUVER
Dave Ceddia | Sciencx - » Delete Git Branches That Have Been Merged. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/08/04/delete-git-branches-that-have-been-merged/
CHICAGO
" » Delete Git Branches That Have Been Merged." Dave Ceddia | Sciencx - Accessed . https://www.scien.cx/2022/08/04/delete-git-branches-that-have-been-merged/
IEEE
" » Delete Git Branches That Have Been Merged." Dave Ceddia | Sciencx [Online]. Available: https://www.scien.cx/2022/08/04/delete-git-branches-that-have-been-merged/. [Accessed: ]
rf:citation
» Delete Git Branches That Have Been Merged | Dave Ceddia | Sciencx | https://www.scien.cx/2022/08/04/delete-git-branches-that-have-been-merged/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.