3

I have a relatively large collection of remote branches from an old remote repo:

$ git branch -r
  guy/feat1
  guy/feat2
  guy/feat3
  guy/feat4
  guy/feat5
  guy/feat6
  guy/feat7
  guy/feat8
  origin/HEAD
  origin/master

Is there one command that would remove all guy branches?

The repo no longer has guy as a remote repo.

eoinoc
  • 3,155
  • 3
  • 24
  • 39

3 Answers3

4
git branch -r | grep guy/ | xargs git branch -d

(Assuming the $ signs aren't actually part of your output...)

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • I'm accepting this as the correct answer, as it looks to work. However, I had greater trouble. When running this command, I got messages `error: branch 'guy/feat1' not found.`. Even changing `-d` to `-D` didn't help. Eventually the following answer **did** work for me: http://stackoverflow.com/a/11050880/248220 – eoinoc Mar 17 '13 at 15:57
  • 1
    A more Git way of iterating like this would be `git for-each-ref --format='%(refname)' 'refs/remotes/guy/*' | xargs git branch -d` – kostix Mar 18 '13 at 07:54
4

You can also try (from git remote):

git remote --prune guy

With:

prune

Deletes all stale remote-tracking branches under <name>.
These stale branches have already been removed from the remote repository referenced by <name>, but are still locally available in "remotes/".

With --dry-run option, report what branches will be pruned, but do not actually prune them.

See also "Difference between git remote prune and git branch -d -r"

if guy is no longer a valide remote repo, then:

git gc --prune=now

will clean up those branches (along with some unreferenced commits, so use it with caution)
See more at "How do you Remove an Invalid Remote Branch Reference from Git?": it is usually more safe to just go with: git branch -rd guy/badbranch if possible, but if this doesn't work, then git gc --prune=now can also be a solution.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I looks like this command is not relevant when `guy` is no longer available remotely. Running the command results in: `fatal: 'guy' does not appear to be a git repository. fatal: The remote end hung up unexpectedly`. I guess the logic is the command goes to check which branches are no longer at that remote repo. – eoinoc Mar 17 '13 at 15:47
  • I had already run `git gc -prune=now`. It looked like it compressed the repo, but the remote branches remained. – eoinoc Mar 17 '13 at 15:55
0

To delete a remote branch you can use git push <remote-repo> :branch-to-delete note the colon before branch-to-delete. Also see Delete multiple remote branches in git ... This shows how to construct a one-liner to delete multiple branches in one go.

Community
  • 1
  • 1
Gavin
  • 1,223
  • 15
  • 20