20

When I list my branches with git branch -a, git shows me remote branches I can't actually find on my remote(gitlab). I tried to checkout and track those branches, and to my big surprise, git did it. But when I do for example git push origin :branch_name, git says that the remote ref does not exist, thus proving that I do not have those branches in my remote. Hence I have a question, are those branches stored in some kind a local cache? And if yes, how to clear it?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
vcmkrtchyan
  • 2,536
  • 5
  • 30
  • 59

4 Answers4

41

Use git fetch -p to prune your local caches

mproffitt
  • 2,409
  • 18
  • 24
8

Like mentioned on cleaning up old remote git branches, you should use git remote prune origin.

I didn't use the other two commands mentioned there.

apaderno
  • 28,547
  • 16
  • 75
  • 90
Jan
  • 1,004
  • 6
  • 23
3

I had a weird issue on macOS where I would get phantom Uppercase versions of branches, e.g. my remote branch listing would show Ben/some-branch locally, but the server would show ben/some-branch.

git remote prune origin would (correctly) remove them, but git fetch --prune would (incorrectly) bring them right back. Very weird.

Best I can tell, at some point in the past, I had a Ben/something branch, but because the default macOS filesystem is case-insensitive, the .git metadata was hanging onto a (capitalized) .git/refs/remotes/origin/Ben folder and using it for the (lowercase) remote branch refs.

rm -rf .git/refs/remotes/origin/Ben && git fetch put me back in order.

Ben Mosher
  • 13,251
  • 7
  • 69
  • 80
0

You can clear the deleted branch data from your Git repository using the git gc command.

git gc

By default, git gc will clean up any unreachable objects and perform other optimizations. If you want to run a more aggressive garbage collection operation, you can use the --aggressive option:

git gc --aggressive
Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144