Git Landscaping

I recently worked on a project with 60+ old feature branches. Most of them had been merged into master and were subsequently abandoned. We decided we wanted to clean up a bit, and git made that easy. Assuming your main branch is named master, and the remote you are cleaning up is called origin:

# Be sure everything is up to date.
git checkout master
git pull

# Remove remote tracking branches which no longer exist.
# Pass -n for a dry run.
git remote prune origin

# See what branches are merged.
git branch -r --merged master

--merged shows you branches that are merged into the named branch. Combine that with -r, which shows you all remote branches, and you can see every merged remote branch.

You can get rid of each of these branches with:

# Remote
git push origin :branch-name

# Local
git branch -d branch-name

Mr. Clean

Branch Prefixing

You can avoid this branch overload by having everyone append their initials to the beginning of every feature branch they create (i.e. js-feature-name). This has the following benefits:

  • You know who is responsible for deleting each branch.
  • You can find all your branches quickly with tab completion.
  • You can easily find a teammate’s branch with only a general idea of its name.

These are some pretty simple concepts, but it is easy to let your git repo get out of control if you don’t stay on top of it. Hopefully, this will help you prune your overgrown git tree for a prim and proper lawn.

Be sure to check out Dan Croak’s post for a more comprehensive look at remote branches, and let us know in the comments how you keep your repo clean.

Hound automatically reviews Ruby, JavaScript, and CoffeeScript code in your GitHub pull requests and comments on style violations. It is free for open source repos and $12/month per private repo.