9

I have ran

git fetch upstream +refs/pull/*:refs/remotes/upstream/pr/*
git fetch origin +refs/pull/*:refs/remotes/origin/pr/*

And I ended up with several hundreds of branches - 4 for each pull request (head, merge, and both from origin and upstream.

How can I get rid of these branches locally, in two steps?

I have tried

git branch -D refs/remotes/origin/pr/*

but that says "not found".

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277

3 Answers3

8

I usually just remove files from .git/refs and lines .git/packed-refs

max630
  • 8,762
  • 3
  • 30
  • 55
3

AFAIK there is no "out of the box" command which does what you want but it can be solved by chaining some bash commands.

The command chain to delete the local branches could look like this:

git branch | grep -vE '^\*' | grep 'pr/' | xargs git branch -D

The version for the remote branches is very similar and uses the -r option for the git branch calls.

git branch -r | grep -vE '^\*' | grep 'pr/' | xargs git branch -rD
Sascha Wolf
  • 18,810
  • 4
  • 51
  • 73
  • whoa! thanks, works like a charm! you just need to add a remote name. i've just deleted 'main-rt' remote branch from 'origin' remote which I do not need in my clone of 'origin': `git br -Dr origin/main-rt`. The result was: `Deleted remote-tracking branch origin/main-rt (was 8488c870869b).`. – vladis Mar 09 '23 at 09:33
2

Following "Can you delete multiple branches in one command with Git?":

git branch -D $(git for-each-ref --format="%(refname:short)" refs/remotes/origin/pr/)
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This seems to be quite close to what I need, but in fact, these branches are remote, and I probably need to remove the information about the remote branches, but not the branches themselves. – Ondra Žižka Oct 05 '15 at 12:36
  • To be specific about why I want this: The hundreds of remote branches clutter all the ways to manage branches - GUIs, logs, command line tab completion, etc. – Ondra Žižka Oct 05 '15 at 12:37