Skip to content

Instantly share code, notes, and snippets.

@GiovanniFrigo
Last active March 3, 2020 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GiovanniFrigo/7087603fade335b6fb08f76ce263d5ec to your computer and use it in GitHub Desktop.
Save GiovanniFrigo/7087603fade335b6fb08f76ce263d5ec to your computer and use it in GitHub Desktop.
Pruning merged branches on a git repository

Pre: ensure you are on your main branch (usually master or devel) and that everything is up-to-date with your origin

git checkout master
git fetch

To list all the merged branches, use

git branch -r --merged | grep -v master | grep -v devel

Note the use of grep to esclude merged branches that should be kept (master, devel)

Then we use sed to remove the origin/ part, and feed each branch name to git push --delete origin [branchname].

The resulting command is:

git branch -r --merged | grep -v master | grep -v devel | sed 's/origin\///' | xargs -n 1 git push --delete origin

You can also use this version to delete your local merged branches. Same deal, move to your master branch and use:

git branch --merged | grep -v master | grep -v devel |  xargs -n 1 git branch -d

Gist based on this StackOverflow answer: https://stackoverflow.com/a/18143078/2304450 and integrated with the info from the comments

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