21

The command git branch -a lists a bunch of branches that are NOT on the repository, and NOT local branches. How can these be deleted?

* develop
  master
  remotes/origin/cloner

For example, remotes/origin/cloner used to exist in the repo, but it has since been deleted and I'd like it not to appear when typing git branch -a.

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
Soroush Hakami
  • 5,226
  • 15
  • 66
  • 99

5 Answers5

30

If you have remote-tracking branches (such as origin/cloner in this case) which are left over after the corresponding branch has been deleted in the remote repository, you can delete all such remote-tracking branches with:

git remote prune origin

The documentation for git remote explains this as:

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

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

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
Alexander Gladysh
  • 39,865
  • 32
  • 103
  • 160
3

To delete a branch which is not needed anymore you can use the following command :

git branch -d -r origin/cloner
aleroot
  • 71,077
  • 30
  • 176
  • 213
0

If you want to keep main and develop branch and delete all local branch from git repository then this might be helpful.

Delete all local branch except main and develop:

git branch -a | egrep -v "(^\*|main|develop)" | xargs git branch -d

darth vader
  • 501
  • 8
  • 11
0

You also do

git push origin :cloner 

To remove unwanted remote branches

Patrick
  • 4,186
  • 9
  • 32
  • 45
  • In this case, the branch `cloner` has already been deleted from the remote repository, so this will produce an error. The question is asking how to remove the remote-tracking branch `origin/cloner`, which is left over after someone has deleted `cloner` in `origin`. – Mark Longair Nov 01 '11 at 21:29
  • I see. Then, wouldn't a git fetch just refresh the remote branch list? – Patrick Nov 02 '11 at 16:19
-5

It may also happen that the remote repository reference has been deleted from the local clone, but still appears in the output of the 'git branch -a' command. In any case, you can always suppress any reference simply by deleting the corresponding files:

$ rm -f .git/refs/remotes/cloner
$ rm -rf .git/refs/remotes/deprecated_remote
Denis Arnaud
  • 388
  • 1
  • 4
  • 7
  • It's a bad idea to directly delete files from under `.git` rather than using porcelain commands to do so. – Mark Longair Nov 01 '11 at 12:30
  • Git is quite robust. It is most often very handy to have to edit the .git/config file, for instance to add tracking for a given branch or to change the URL of a remote repository. By looking directly under the hood, once can understand more easily how Git works, and realise that it is as magic as it appeared the first time. So, I would not recommend that everybody edits the Git files under the hood, but I would recommend that they at least have a loot at those files. At the mininum, it is very instructive. – Denis Arnaud Nov 05 '11 at 12:24