92

When I use bash autocompletion in Git, it keeps showing me branches of old remotes that I don't have anymore. When I do a git branch -la it shows those old remotes and branches while a git branch -l won't. A ls .git/refs/remotes/ also shows them. However, they are not present in my .git/config and neither are they shown when I run git remote show.

So how do I get rid of them because my autocomplete list is too long right now.

I have already tried:

git reflog expire --expire=now --all
git gc --prune=now
rm .git/refs/remotes/theoldremote
git remote prune theoldremote

I'm also aware of the fact that I can just re-clone the repo but that's just cheating ;-)

Alex
  • 2,953
  • 1
  • 27
  • 37
  • Note: a `git remote rm` now (git 2.0.1, June 2014) delete first the remote tracking branches. That should help in avoiding cleaning up old branches. See [my answer below](http://stackoverflow.com/a/24984473/6309) – VonC Jul 27 '14 at 18:59
  • http://stackoverflow.com/questions/8766525/git-branch-av-showing-remote-branch-that-no-longer-exists – Dan K.K. May 31 '16 at 17:38
  • Here is my another answer : https://stackoverflow.com/a/44129766/3835843 – Arif Sep 14 '17 at 07:41

6 Answers6

152

Git does not delete the (local) remote-tracking branches automatically if the branch was deleted in the remote repository. Additionally, before V2.0.1 remote-tracking branches were in some cases not deleted when you removed the remote from your git config (see VonC's answer).

To delete stale remote-tracking branches (branches that were deleted in the remote repository) for one of your remote repositories, run

git remote prune <remote>

To cite the man page or git remote:

prune

Deletes all stale 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/<name>".

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

However, from your question it seems you manually removed .git/refs/remotes/theoldremote, so Git no longer knows about the remote repository that the remote-tracking branches belonged to. That's not how you're supposed to do it.

The normal way to remove a remote repository is to run

git remote rm <remote>

This will remove the remote from your .git/config, and will delete the remote-tracking branches.

If you just delete the directory under .git/refs/remotes/, the branches will remain behind. Then you will need to remove them manually:

git branch -rd <remote>/<branchname>

You need option -r to delete a remote branch.

sleske
  • 81,358
  • 34
  • 189
  • 227
  • 2
    Nope, that's not it. It says `fatal: 'kolichikov' does not appear to be a git repository`. – Alex Jul 04 '13 at 14:03
  • 2
    For automatic pruning of remote branches on fetch/pull, see also: http://stackoverflow.com/a/18718936/968201 – Patrick James McDougle Sep 11 '14 at 18:47
  • If you find this slow, upgrade to the newer Git (2.0.1+ IIRC) where this is fixed (over 100x faster). – odinho - Velmont Dec 12 '14 at 10:34
  • Note to prune users : [Git documentation](https://git-scm.com/docs/git-prune) says "_In most cases, users should run `git gc`, which calls git prune._". However note that `git gc` is not compatible with `--dry-run`. – JYL Jan 10 '17 at 09:00
  • `git branch -rd /` was what I was looking for. Thank you! – Antoine Colson Aug 01 '20 at 16:39
17

I use

git push origin :remote_branch

to remove a branch from server.

git remote prune origin

to remove remote references which do not exist on server anymore

Mert
  • 6,025
  • 3
  • 21
  • 33
  • Nope, that's not it. It says `fatal: 'kolichikov' does not appear to be a git repository` – Alex Jul 04 '13 at 14:05
  • 1
    Alex, you have to run the command inside the repository folder not in your home folder... – SparK Feb 14 '19 at 16:58
  • Worked very well for me; what remained were stale branches I subsequently cleaned up too :-) – Murphy Jan 27 '23 at 18:30
6

Note: while git remote prune is the answer, know that, starting with git 2.0.1 (June 25th, 2014), a git remote rm starts by removing the remote tracking branches.
So hopefully, one shouldn't have to cleanup old branches after a git remote rm.

See commit b07bdd3 by Jens Lindström (jensl)

remote rm: delete remote configuration as the last

When removing a remote, delete the remote-tracking branches before deleting the remote configuration.
This way, if the operation fails or is aborted while deleting the remote-tracking branches, the command can be rerun to complete the operation.


But if you have to, a simple git fetch can be enough, provided you have set first:

git config --global fetch.prune true
cd /path/to/repo
git config remote.origin.prune true
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • See also https://github.com/git/git/commit/c9e768bb77762765f739fffb5c09d0888f3de20e – VonC Jul 27 '14 at 19:04
5

Push nothing to a branch to delete it:

git push remote :remote_branch

It's somewhere in the docs but it isn't really obvious.

Or did I misunderstand your question?

aragaer
  • 17,238
  • 6
  • 47
  • 49
  • 3
    "It's somewhere in the docs but it isn't really obvious." such is `git` – mnagel Jul 04 '13 at 12:50
  • By "remote" i meant your remote repository name. Such as "origin" or whatever. But I'm starting to think that you meant something else than simply deleting the remote branch. – aragaer Jul 04 '13 at 12:50
  • Something else such as this: http://stackoverflow.com/questions/1072171/how-do-you-remove-an-invalid-remote-branch-reference-from-git – aragaer Jul 04 '13 at 12:53
4

Ok I've got it. The problem was that the remotes don't exist anymore, but they do somewhere in the git database. I re-added the remotes, then did

git remote prune theremote
git remote rm theremote
git gc --prune=now

After that they disappear from the list. Somehow I didn't remove them correctly before I guess.

Alex
  • 2,953
  • 1
  • 27
  • 37
  • I tried everything from a bunch of sources, and the only thing that worked was to re-add the problem remote and then remove it. Thanks! – jc00ke Feb 03 '17 at 01:42
1

I was getting confused when remote branches that had been deleted on the server side were still appearing when I ran:

$ git branch --all

The following command fixed this for me (on git version 2.25.0):

$ git remote update --prune
htaccess
  • 2,800
  • 26
  • 31