Skip to content

Instantly share code, notes, and snippets.

@rsrchboy
Created April 12, 2011 19:29
Show Gist options
  • Save rsrchboy/916208 to your computer and use it in GitHub Desktop.
Save rsrchboy/916208 to your computer and use it in GitHub Desktop.
git-trim-fork
#!/usr/bin/env perl
# A small scriptie to clean up all those branches that get carried over
# after a github fork that (99% of the time) end up cluttering everything up
# and obscuring your own branches :)
#
# This script is under the GNU GPL, either version 2 or (at your option) any
# later version.
#
# Chris Weyl <cweyl@alumni.drew.edu> 2010.
use common::sense;
use opts;
use Git;
#use IO::Prompt;
use Pod::Usage;
opts
my $help => { isa => 'Bool', default => 0 },
;
pod2usage if $help;
my $repo = Git->repository;
my @origin_branches = sort
grep { !/^HEAD / && $_ ne 'master' }
map { s!^\s+remotes/origin/!!; $_ }
grep { /^\s+remotes\/origin/ }
$repo->command(qw{ branch -a })
;
my %local_branches =
map { s/^\**\s+//; $_ }
$repo->command('branch')
;
my @local_branches = keys %local_branches;
for my $remote_branch (@origin_branches) {
if ($remote_branch ~~ @local_branches) {
say "$remote_branch has a local branch; not deleting";
next;
}
# git-branch prints its bits to STDERR
$repo->command(qw{ push origin }, ":$remote_branch");
}
__END__
=head1 NAME
git-trim-fork - Cleanup branches from a Github fork
=head1 SYNOPSIS
git trim-fork [--help]
=head1 DESCRIPTION
Every once in a while, for one reason or another it becomes prudent to fork a
GitHub repository. Of course, when you fork that repository, you get all of
the branches in that repository. Even if you don't need or want any of them.
Even if there are 30 of them.
To add to it, it may be a popular repository, so the next time you git-sync
you get all 30 of their copies of the branches. From each fork. Oye.
trim-fork is a git command that runs through your list of remote branches
(matching remotes/origin/*), checks to see if you have a local branch of the
same name, and removes any that don't. (We also don't remove HEAD or master,
even if you don't have a local branch/ref.)
So do your part. Be a good, upstanding githubian! Purge your unneeded
branches!
=head1 AUTHOR
Chris Weyl <cweyl@alumni.drew.edu>
=head1 LICENSE AND COPYRIGHT
This script is under the GNU GPL, either version 2 or (at your option) any
later version.
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment