Git housekeeping tutorial: clean-up outdated branches in local and remote repositories

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:We need to know what branches are already merged in “master” and can be easily removed:Now, remove all outdated branches with:Next, decide what to do with not merged branches:If some of them is just abandoned stuff that you don’t need anymore, remove it with “-D” option:

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:Clean-up outdated references:

Tip

Update repository with: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”: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:Now, you can delete own remote branches, and ask other authors to clean-up theirs:Similar snippet for not merged branches:This list should be reviewed more thoroughly to avoid losing important commits.

Tip for Github users

After the last Github update, Branches page is divided into “Your branches”, “Active branches” and “Stale branches”, and it shows same information as previous commands.

Bonus snippets

Usually, it’s simple to remove local and appropriate remote branches at once.This snippet shows only local merged branches, which have appropriate remote merged branches:More hardcore snippet with date and author information:Same snippets for not merged branches:

Moving stuff into .gitconfig

It’s hard to remember such code, so the best way is to create shell scripts and put them in local “bin” folder. Note that these snippets work only in bash(and zsh). For example, let’s put first snippet into “bin/git-both-merged” file:Don’t forget to make it executable( chmod 755 git-both-merged), and you can also make a git alias for this script. Put next line in .gitconfig:Now you can call it via git command:In the same vein you can create git aliases for all snippets from this article.That’s it, folks. Now, it’s time to clean-up all stale stuff!