0

For example, if you run git remote add repository and then git fetch repository it will populate all branches found on the repo. Is there a way to reset the list of branches to the same as when you initially added it, with none fetched?

PHPStorm by default lists every remote branch that has been fetched. This list has gotten rather long and I would like to remove listed branches without actually deleting them or the remote (so if I do want them back in the future it won't require me re-adding the repo).

sailboatlie
  • 346
  • 1
  • 6
  • 18

3 Answers3

4

Came up with the below solution to empty out the list of remote branches for a repo. Similar to Thomas' solution but still using entirely git to loop through and delete all remote branches:

git branch -d -r $(git for-each-ref --format="%(refname:short)" refs/remotes/**repository**/)
sailboatlie
  • 346
  • 1
  • 6
  • 18
  • 2
    Beautiful! I never knew about `for-each-ref`. Remember that you're allowed to accept your own answer after 48 hours... And you should *definitely* do that here :D – Thomas Kelley Dec 03 '13 at 00:33
1

You can remove the local representation of a remote branch, without removing the branch remotely, by using the -d (delete) and -r (remote) flags on a git branch call:

git branch -d -r origin/some-feature-branch

Don't know what OS/shell you're using, so I'll leave you to create a loop to run through and remove these.

Be warned that the next time you fetch that remote (via git fetch origin or git fetch --all), that the branches will be re-created locally, unless you specify the branch to fetch, like this:

git fetch origin some-other-feature-branch
Thomas Kelley
  • 10,187
  • 1
  • 36
  • 43
0

I don't know of a way to remove many remote branches in one command via git, but a branch is nothing more than a text file with the name of the branch, containing the hash of the commit at the head of that branch. The repository remote branches would just be text files in your project's .git/refs/remotes/repository folder. You can simply delete those text files. The branches won't show up anymore.

This is sidestepping git's control mechanisms, but that is literally all that branches are. I've done this before, but won't guarantee your safety if you go mucking about in your .git folder, so be careful, and back things up if you're not completely certain. Doing a git fetch <repository> will restore the branch pointers.

Gary Fixler
  • 5,632
  • 2
  • 23
  • 39