Skip to content

Commit 859607d

Browse files
spearceJunio C Hamano
authored andcommitted
Teach 'git remote' how to cleanup stale tracking branches.
Since it can be annoying to manually cleanup 40 tracking branches which were removed by the remote system, 'git remote prune <n>' can now be used to delete any tracking branches under <n> which are no longer available on the remote system. Signed-off-by: Shawn O. Pearce <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7a8c9ec commit 859607d

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

Documentation/git-remote.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ SYNOPSIS
1212
'git-remote'
1313
'git-remote' add <name> <url>
1414
'git-remote' show <name>
15+
'git-remote' prune <name>
1516

1617
DESCRIPTION
1718
-----------
@@ -26,6 +27,10 @@ update remote-tracking branches <name>/<branch>.
2627

2728
In the third form, gives some information about the remote <name>.
2829

30+
In the fourth form, deletes all stale tracking branches under <name>.
31+
These stale branches have already been removed from the remote repository
32+
referenced by <name>, but are still locally available in "remotes/<name>".
33+
2934
The remote configuration is achieved using the `remote.origin.url` and
3035
`remote.origin.fetch` configuration variables. (See
3136
gitlink:git-config[1]).

git-remote.perl

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ sub show_mapping {
200200
print " @$new\n";
201201
}
202202
if (@$stale) {
203-
print " Stale tracking branches in remotes/$name (you'd better remove them)\n";
203+
print " Stale tracking branches in remotes/$name (use 'git remote prune')\n";
204204
print " @$stale\n";
205205
}
206206
if (@$tracked) {
@@ -209,6 +209,23 @@ sub show_mapping {
209209
}
210210
}
211211

212+
sub prune_remote {
213+
my ($name, $ls_remote) = @_;
214+
if (!exists $remote->{$name}) {
215+
print STDERR "No such remote $name\n";
216+
return;
217+
}
218+
my $info = $remote->{$name};
219+
update_ls_remote($ls_remote, $info);
220+
221+
my ($new, $stale, $tracked) = list_mapping($name, $info);
222+
my $prefix = "refs/remotes/$name";
223+
foreach my $to_prune (@$stale) {
224+
my @v = $git->command(qw(rev-parse --verify), "$prefix/$to_prune");
225+
$git->command(qw(update-ref -d), "$prefix/$to_prune", $v[0]);
226+
}
227+
}
228+
212229
sub show_remote {
213230
my ($name, $ls_remote) = @_;
214231
if (!exists $remote->{$name}) {
@@ -270,6 +287,25 @@ sub add_remote {
270287
show_remote($ARGV[$i], $ls_remote);
271288
}
272289
}
290+
elsif ($ARGV[0] eq 'prune') {
291+
my $ls_remote = 1;
292+
my $i;
293+
for ($i = 1; $i < @ARGV; $i++) {
294+
if ($ARGV[$i] eq '-n') {
295+
$ls_remote = 0;
296+
}
297+
else {
298+
last;
299+
}
300+
}
301+
if ($i >= @ARGV) {
302+
print STDERR "Usage: git remote prune <remote>\n";
303+
exit(1);
304+
}
305+
for (; $i < @ARGV; $i++) {
306+
prune_remote($ARGV[$i], $ls_remote);
307+
}
308+
}
273309
elsif ($ARGV[0] eq 'add') {
274310
if (@ARGV != 3) {
275311
print STDERR "Usage: git remote add <name> <url>\n";
@@ -281,5 +317,6 @@ sub add_remote {
281317
print STDERR "Usage: git remote\n";
282318
print STDERR " git remote add <name> <url>\n";
283319
print STDERR " git remote show <name>\n";
320+
print STDERR " git remote prune <name>\n";
284321
exit(1);
285322
}

0 commit comments

Comments
 (0)