Skip to content

Instantly share code, notes, and snippets.

@everdimension
Created April 17, 2019 12:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save everdimension/2e9b4fe9e7c994f5652169a100ee7c48 to your computer and use it in GitHub Desktop.
Save everdimension/2e9b4fe9e7c994f5652169a100ee7c48 to your computer and use it in GitHub Desktop.
Different git branch cleanup approaches

Cleanup merged branches

Source: https://stackoverflow.com/a/6127884/3523645

git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d

Cleanup orphaned branches

Problem: If pull-request are merged into the main branch using a "squash" strategy and then the remote branches are removed, local branches will not show up as "merged" (git branch --merged will not show them).

These branches can still be sorted out (https://stackoverflow.com/a/47939403/3523645)
First, we need to delete remote refs that are no longer in use on the remote repository:

git fetch --prune

Now, git will know which local branches have their remotes deleted. So if we do this:

git branch --verbose

We will see those branches marked with [gone].

To remove them, we can run something like this:

git branch --verbose | grep "\[gone\]" | cut -d " " -f3 | xargs git branch -D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment