Skip to content

Instantly share code, notes, and snippets.

@aaronsummers
Created July 29, 2022 14:47
Show Gist options
  • Save aaronsummers/782ff61454a791bdc7202fb0b5f562b1 to your computer and use it in GitHub Desktop.
Save aaronsummers/782ff61454a791bdc7202fb0b5f562b1 to your computer and use it in GitHub Desktop.
Cleaning up Git Branches

Source

After working with branch per feature for a while any Git-repository becomes a mess of outdated and not finished branches.

To deal with this issue, we need to clean-up three kinds of branches:

  • Local branches – our day-to-day working branches
  • References to remote branches – aka origin/branch-name items
  • Actual remote branches – branches on remote server(e.g.: github, bitbucket, gitorius)

In this tutorial we suppose, that “master” – is a default branch, where everything is merged (for someone it could be “develop”, or “release” branch), and “origin” – it’s a name of remote. If you use different names, just change them in appropriate commands.

Local branches

At first, list all local branches:

$ git branch

We need to know what branches are already merged in “master” and can be easily removed:

$ git checkout master $ git branch --merged

Now, remove all outdated branches with:

$ git branch -d old-merged-feature

Next, decide what to do with not merged branches:

$ git branch --no-merged

If some of them is just abandoned stuff that you don’t need anymore, remove it with “-D” option:

$ git branch -D old-abandoned-feature

References to remote branches

After each git pull or git fetch command Git creates references to remote branches in local repository, but doesn’t clean up stale references.

List referenced remote branches:

$ git branch -r

Clean-up outdated references:

$ git remote prune origin

Tip

Update repository with:

$ git fetch -p

and Git automatically prunes all stale references. Remote branches

Usually, remote repository is a big garbage heap of stale branches, if there is no responsible housekeeping person.

After previous git remote prune origin we should have synched list of remote branches.

At first, we can find branches which are already merged in “master”:

$ git checkout master $ git branch -r --merged

But this command does not provide much information. What if this branch is merged, but still used for feature development. Would be cool to know last commit date and author.

This magic snippet provides all required information:

$ for branch in `git branch -r --merged | grep -v HEAD`; do echo -e `git show --format="%ci %cr %an" $branch | head -n 1` \\t$branch; done | sort -r

Now, you can delete own remote branches, and ask other authors to clean-up theirs:

$ git push origin --delete branch-name

Similar snippet for not merged branches:

$ for branch in `git branch -r --no-merged | grep -v HEAD`; do echo -e `git show --format="%ci %cr %an" $branch | head -n 1` \\t$branch; done | sort -r

This list should be reviewed more thoroughly to avoid losing important commits.

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