How do I remove deleted branch names from autocomplete?

Git

Git Problem Overview


I used git branch -d myBranch to delete a branch. However, when I am on master and try to checkout a new branch with git checkout, myBranch still appears in the tab-autocomplete.

How do I remove the name myBranch from tab-autocomplete for git checkout?

Git Solutions


Solution 1 - Git

git fetch --prune --all

Posting this as its own answer since it's a one-line fix, but if you vote be sure to vote for @twalberg's answer above.

@twalberg's suggestion to git branch -a led me on the right track; my coworker suggested git fetch --prune --all to prune all the dead branches from all the remotes, which is useful when working with lots of devs with lots of forks.

Solution 2 - Git

One possible reason for this is that, if a remote branch (e.g. origin/myBranch) still exists, then git checkout myBranch will succeed as an alternative to git checkout -b myBranch origin/myBranch. This is intended as a convenience for the common case of checkout out a remote branch for the first time, creating an identically named local tracking branch.

There are other possibilities, too, depending on what exactly you are using for completion, but that's one of the first things I'd check. If you run git branch -a, and there is an origin/myBranch listed (or one for a remote other than origin, if you have such), then that's a likely culprit.

Solution 3 - Git

Depending on your setup, there is another source of what might look like old or deleted branches - your git-completion may also be suggesting tags along with branches.

I was fooled by this recently - our CI/CD pipeline tags all our builds, and even though certain branches would be released and/or deleted years ago, the tags persisted. Cleaning up old tags on the remote was the solution here (there is a guide here).

(I answered this here as well - not sure if it counts as a duplicate as it's on StackExchange...?)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn HoffmanView Question on Stackoverflow
Solution 1 - GitericsocoView Answer on Stackoverflow
Solution 2 - GittwalbergView Answer on Stackoverflow
Solution 3 - GitrocksteadyView Answer on Stackoverflow