6

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
  • 40,240
  • 36
  • 196
  • 259

3 Answers3

7

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

max630
  • 7,979
  • 3
  • 27
  • 52
2

AFAIK there is no "out of the box" command which does what you want but it can be solved by simply 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 merely use the -r option for the git branch calls.

git branch -r | grep -vE '^\*' | grep 'pr/' | xargs git branch -rD
Sascha Wolf
  • 17,034
  • 4
  • 47
  • 72
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,129,465
  • 480
  • 4,036
  • 4,755
  • 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