Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save boywijnmaalen/320bd1a72f9b4d78a29c to your computer and use it in GitHub Desktop.
Save boywijnmaalen/320bd1a72f9b4d78a29c to your computer and use it in GitHub Desktop.
Remove local branches except the master branch
###########
# DRY-RUN #
###########
# make sure you are on develop; one cannot ever delete the current branch
$ git checkout develop
# get references
$ git fetch
# fast forward (not merge) the current branch
$ git pull origin HEAD
# list every local branch (except 'develop' & 'master') that has already been merged with the local develop
$ git branch --merged develop | grep -vE 'develop|master'
# list stale references (like branches, tags, unreachable commits, etc)
$ git remote prune origin --dry-run
###########
# EXECUTE #
###########
# make sure you are on develop; one cannot ever delete the current branch
$ git checkout develop
# get references
$ git fetch
# fast forward (not merge) the current branch
$ git pull origin HEAD
# delete every local branch (except 'develop' & 'master') that has already been merged with the local develop
$ git branch --merged develop | grep -vE 'develop|master' | xargs git branch -d
# delete all stale tracking branches.
$ git gc --prune=now
# de-reference stale references (like branches, tags, unreachable commits, etc)
$ git remote prune origin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment