6

I've deleted some local branches via git branch -d branchname, but they are still exist in autocomplete (when I put git checkout, then press tab key, I see all deleted branches in list).

I've tried to make git gc and git prune, but nothing changes.

  • Have you tried commiting and pushing your changes? you can try `git branch -D the_local_branch`. But be careful. This will delete the branch even if it not merged. – TheOneWhoPrograms Mar 03 '14 at 13:14

6 Answers6

17

TL;DR - the real answer:

$ git remote prune origin

Slightly more details:

git branch -d foo-branch will fail to delete if foo-branch is not merged into your current branch.

It's also a good idea to clean up old branches on a remote if they've already been merged. You can do this from the commandline by git push origin :foo-branch.

However, the old "phantom" branch foo-branch will linger for autocompleting git checkout. Apparently git is caching the branches available on the remote. By calling:

$ git remote prune origin

You can clear the local cache of what branches are available on the git remote.

I can reproduce the OP's issue for git 2.2.1 (and probably earlier versions) on OSX.

Rustavore
  • 1,845
  • 2
  • 23
  • 31
2

If you are sure that you do not want the branch, you can remove it from your local and the remote like this:

$ git branch -D branchname
$ git push origin :branchname

Then it will stop appearing in autocomplete results.

Agis
  • 32,639
  • 3
  • 73
  • 81
  • I've pushed changes to the `origin/branchname`, then the changes were merged with `origin/master` and `origin/branchname` were deleted on the remote. I still see both `branchname` and `origin/branchname` in the autocomplete. `origin/branchname` is ok, I want to delete only local, because there is too many deleted branches. – user1486054 Mar 03 '14 at 13:59
  • If you want to delete the local then run only the 1st line of my answer. – Agis Mar 03 '14 at 14:00
  • The braches are actually deleted and `git branch -a` doesn't show anything. The problem is only in checkout autocomplete. – user1486054 Mar 03 '14 at 14:03
  • Yes, because it also exists in the remote repository. – Agis Mar 03 '14 at 14:05
  • 4
    I got it, I just had to launch `git remote prune origin` to update local repo. Anyway, thanks for the answers! – user1486054 Mar 03 '14 at 14:12
0

You've probably tried to delete an unmerged branch, so git branch -d branchname has not deleted it. You can check if the branch still exists with:

git branch -a

If you're really sure to delete the ~brancheven if is notmerged` you can force the deletion with:

git branch -D branchname
Atropo
  • 12,231
  • 6
  • 49
  • 62
  • `git branch -a` shows only remote branches. Local branches that was deleted I can see only in checkout autocomplete. – user1486054 Mar 03 '14 at 14:02
0

I had this problem and the previous solutions didn't work for me. It turned out that I had a tag in the repo with the same name as the old deleted branch. You can delete the tag with:

git tag -d [tagname]

To delete it remotely:

git push origin :refs/tags/[tagname]

Thanks to https://nathanhoad.net/how-to-delete-a-remote-git-tag

adambrod
  • 423
  • 3
  • 7
0

Note that pushing the branch deletion (git push origin :branchname) is easier in Git 2.13 (Q2 2017), since the completion script learned to complete "git push --delete b<TAB>" (to complete branch name to be deleted).

See commit 723c1d5 (22 Apr 2017) by Ævar Arnfjörð Bjarmason (avar).
(Merged by Junio C Hamano -- gitster -- in commit f70b541, 26 Apr 2017)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
-1

I realize this is really, really old, but I just ran into this issue, and in my case, I had pruned the branches in origin, but not the ones in origin-http.

I don't normally use http for cloning, so this is not typically an issue, but...welp.

Once I did git remote prune origin-http, it fixed the problem for me.

Mike Loux
  • 706
  • 2
  • 11
  • 23