-1

I see some similar questions on StackOverflow which say to run the command

git branch | grep -v "master" | xargs git branch -D

However this only removes branches which I have checked out at some point. My current issue is I have pulled down 54 thousand branches which I have never checked out by running git fetch and now I want to remove all these.

Here's what I see when I run git checkout [TAB]:

do you wish to see all X possibilities (Y lines)?

enter image description here

genpfault
  • 51,148
  • 11
  • 85
  • 139
Ozymandias
  • 2,533
  • 29
  • 33
  • you're only listing local branches with `git branch`. You need to add the `-r` flag to list branches on the remote. Verify the results are what you want before just plugging that in and running it! – erik258 Mar 06 '23 at 20:31
  • 2
    please don't share text as screenshots. https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors – erik258 Mar 06 '23 at 20:31
  • `git branch -r` still only shows 1 line. Not the 54k branches which have been downloaded as part of the `git fetch` – Ozymandias Mar 06 '23 at 20:33
  • Okay. How can I remove all these indexed branches? – Ozymandias Mar 06 '23 at 20:35
  • 1
    try `git branch -ra` and see what that gives you. You might need to run another `git fetch` – erik258 Mar 06 '23 at 20:36
  • I'll try that out. `git fetch` appears to be enumerating over 168k objects (# of branches must have grown since I ran it last) and appears to take about 5 mins to complete. – Ozymandias Mar 06 '23 at 20:38
  • Unfortunately this does not seem to help. Running `git branch -ra | grep -v "master" | xargs git branch -D` results in a number of error messages such as `error: branch 'remotes/origin/yzhao/COMM-1346-group-exceptions-only' not found.` and repeating `git checkout [TAB]` afterwards still results in the many 10s of thousands of possible results. – Ozymandias Mar 06 '23 at 20:43
  • 1
    I think the issue you're facing is that git checkout tab autocompletes with the thousands of remote branches you could check out. Those are distinct from branches you would delete with `-D`. I found this https://stackoverflow.com/questions/32887385/git-how-to-unfetch-remote-branches-github-pull-requests – erik258 Mar 06 '23 at 21:16
  • Try `git for-each-ref` to see what these possible branches are. There could be some special refs besides branches. – ElpieKay Mar 07 '23 at 01:45
  • 1
    @erik258 Minor nit: `git branch -ra` is redundant. [Option `-a`](https://git-scm.com/docs/git-branch#Documentation/git-branch.txt--a) includes `-r`. So it's either `git branch` for local branches, `git branch -r` for only remote-tracking or `git branch -a` for both. – phd Mar 07 '23 at 09:55

1 Answers1

0

Tab completion of git checkout lists not only branches, but also files. Typing git checkoutTAB is not a good way to count the number of branches.

If

git branch -r

lists only one branch, then that's all the branches you have downloaded.

j6t
  • 9,150
  • 1
  • 15
  • 35
  • Yup looks like even when I do a fresh clone there's still 10s of thousands of autocomplete suggestions. – Ozymandias Mar 06 '23 at 21:52
  • @Ozymandias Then you need to investigate what are those suggestions — are they local branches? remote-tracking branches? tags? or what? – phd Mar 07 '23 at 09:56
  • It seems like the remote repo has 2724 branches and 48777 tags. After enabling infinite scrollback on my terminal I'm seeing `git checkout` shows each branch twice, once with an `origin/` prefix and once without the prefix, then all the tags are listed once. – Ozymandias Mar 07 '23 at 18:42
  • Branches with an `origin/` prefix and without it are local and remote-tracking branches. Tags don't have remote-tracking tags. – phd Mar 07 '23 at 19:29