Blog by Railsware

Git Clean-up in Local and Remote Branches, Repositories

Git clean-up tutorial

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:

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.

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:

$ comm -12  <(git branch --merged|awk '{print($1)}') <(git branch -r --merged|awk '{print($1)}'|awk -F \/ '{print($2)}')

More hardcore snippet with date and author information:

$ for branch in `comm -12  <(git branch --merged|awk '{print($1)}') <(git branch -r --merged|awk '{print($1)}'|awk -F \/ '{print($2)}')`;do echo -e `git show --format="%ci %cr %an" $branch | head -n 1` \\t$branch; done | sort -r

Same snippets for not merged branches:

$ comm -12  <(git branch --no-merged|awk '{print($1)}') <(git branch -r --no-merged|awk '{print($1)}'|awk -F \/ '{print($2)}')
$ for branch in `comm -12  <(git branch --no-merged|awk '{print($1)}') <(git branch -r --no-merged|awk '{print($1)}'|awk -F \/ '{print($2)}')`; do echo -e `git show --format="%ci %cr %an" $branch | head -n 1` \\t$branch; done | sort -r

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:

#!/usr/bin/env bash

comm -12  <(git branch --merged|awk '{print($1)}') <(git branch -r --merged|awk '{print($1)}'|awk -F \/ '{print($2)}')

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:

[alias]
  # ... other aliases
  both-merged = !git-both-merged

Now you can call it via git command:

$ git both-merged

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!

Exit mobile version