List and delete "stray" git branches
Summary
Run git branch--delete--stray
to delete local tracking branches for which the remote branch being tracked has been deleted on the remote... use git branch--list--stray
to just list them; both work best with regular pruning of remote-tracking references!
Background
Git has a default disposition of keeping data unless it's explicitly thrown away; this extends to (unnecessarily?) holding onto references to so-called local "remote-tracking" branches where the branch being tracked has long since been deleted on the remote itself.
This utility refers to these local tracking branches as "stray" branches, and includes two git extensions: one for listing all "stray" branches, and a second one for (selectively) deleting them.
In case of the GitHub Flow for instance, a local branch would become "stray" after a pull request gets merged and the underlying remote branch gets deleted... after the next git fetch
it would be possible to identify the "stray" branch with something akin to git branch --list --merged
, and delete them accordingly.
But that's not the only scenario that results in "stray" branches, which is why the various "solutions" you find on Stack Overflow don't typically cover all edge cases, whereas this utility does; it also prefers git plumbing over git porcelain, and it implements most of its logic in a library, making it very straightforward to implement even more git extensions for dealing with "stray" branches.
What's in a name?
In the context of this utility, a branch is considered "stray" if it is what git calls a "remote-tracking" branch, but one where the remote branch it was tracking no longer exists (ie. where the branch it was tracking has been deleted on the remote, e.g. on GitHub or on Bitbucket).
The term "stray" was chosen to avoid confusion with existing git terminology like ''merged", "tracked" and even "orphaned".
Also, the names of the two git extensions implemented by this utility:
git branch--list--stray
git branch--delete--stray
... were chosen two mimic some existing git branch
options:
git branch --list --merged
git branch --delete
respectively.
Installation
gem install git-branch--stray
Usage
After installing the gem, you will have two new git extensions in your environment: git-branch--list--stray
will list all stray branches, whereas git-branch--delete--stray
will iterate over the list and - after prompting for confirmation - delete any stray branches.
Because they will be in your local $PATH
you can run them as so-called git subcommands, as follows:
git branch--list--stray
git branch--delete--stray
Most likely, though, you will want to create some git aliases to make it easier to work with the two utility scripts:
git config --global alias.bls branch--list--stray
git config --global alias.bds branch--delete--stray
Alternatively, a similar approach can be used to add either of the scripts to your existing git aliases and/or git utility scripts.
Pruning
The two git extensions work best if your git workflow includes regular pruning of remote-tracking references that no longer exist on the remote, either by running git fetch --prune
when syncing with the remote in question, or else configuring git's pruning behaviour for your environment.
Extending
Most of the utility is implemented in a Ruby library, making it very easy to use the logic for dealing with "stray" branches in your own Ruby applications or utilities.
Add this line to your application's Gemfile:
gem 'git-branch--stray'
And then execute:
$ bundle
The library to require is in the lib/git/branch/stray.rb
file.
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/pvdb/git-branch--stray.