Skip to content

Instantly share code, notes, and snippets.

@ryanc414
Last active July 12, 2022 12:17
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ryanc414/f7686d2c97808b41ed8518a5840e2d78 to your computer and use it in GitHub Desktop.
Save ryanc414/f7686d2c97808b41ed8518a5840e2d78 to your computer and use it in GitHub Desktop.
Delete merged git branches locally and on remote
#!/bin/bash
# Remove merged git branches locally and on remote(s)
set -e
# Script constants
BRANCH_WHITELIST="(\*|master|other-special-branches)"
REMOTES="origin" # Add more remotes here as space-separated list
echo "Deleting merged branches locally"
git branch --merged master \
| egrep -v "$BRANCH_WHITELIST" \
| xargs -r git branch -d
# Delete merged branches from each remote.
for remote in $REMOTES; do
echo "Deleting merged branches from $remote"
git fetch "$remote" --prune
for branch in $(git branch -r --merged master \
| grep "$remote" \
| egrep -v "$BRANCH_WHITELIST"); do
git push "$remote" --delete "${branch#*/}"
done
done
@Jogai
Copy link

Jogai commented Aug 27, 2019

This is really useful. The only thing is that line 8 doesnt really work. If I add another remote i get:

fatal: 'origin external' does not appear to be a git repository

For now I just ran it twice 😄

@ryanc414
Copy link
Author

This is really useful. The only thing is that line 8 doesnt really work. If I add another remote i get:

fatal: 'origin external' does not appear to be a git repository

For now I just ran it twice 😄

Thanks!

Oops, I shouldn't have included the quotes in the loop on line 16 - try removing them and hopefully it should work for multiple remotes!

@ryanc414
Copy link
Author

To run this script on macOS, you need to first install the GNU version of xargs via brew install findutils, then change line 13 to call gxargs (or update your PATH so that the GNU version of xargs comes before the BSD version).

@braian87b
Copy link

is there is any harm if I delete merged branches from a fork?
and, if I do the same for unmerged (old, stalled) branches also?
and if I deleted a branch can I put it back again in the fork from upstream? how? Thanks!

@ryanc414
Copy link
Author

No particular harm no, the changes you make to a fork are completely independent of the changes to the upstream. Obviously if you delete an unmerged branch that you created in your fork and isn't mirrored in the upstream, you won't (easily) be able to get it back.

Sure you can pull a branch from upstream into your branch, you just need to pull it from upstream to your local repo first and then push it to your fork. Assuming you have the fork remote set up as "origin" and the upstream set up as "upstream" (git remote add upstream <upstream URL> if you don't have that):

git fetch upstream/branchname
git checkout branchname
git push -u origin branchname

@braian87b
Copy link

@ryanc414 Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment