gitautocompletebranch

Is there a way to safely but thoroughly forget a git branch after it is deleted?


It is handy to be able to press the tab key and get automated command completion, such as to fill out the remainder of the name of a branch. However, if one types...

git switch de[tab]

... and there are two or more branches that begin with the initial fragment (e.g. "de"), then command completion stops. For example, if there is a branch develop and a branch dev-1234, then the above stops at...

git switch dev

This limitation remains the case even after one has deleted all but one of the branches. For example, after deleting dev-1234, the above example still stops with "dev". For command completion purposes, it seems to still remember that there was a branch dev-1234, even if that is no longer in the list of branches.

Of course, it can be desirable to be able to restore a branch that has been mistakenly deleted and there are MANY pages that discuss the recovery process.

But consider the case where the deleted branch has been merged and one does not want to recover or restore it. In this example, is there a safe way to make git command completion forget that there had been a branch called dev-1234 so that command completion can once again expand "de" to "develop"?


Solution

  • git fetch --prune will fetch the latest refs from the origin remote, and then remove any local branches that no longer exist on origin. (You can specify a different remote name after --prune if you want, but origin is the default.) It's functionally equivalent to git fetch origin && git remote prune origin.

    If you want pruning to happen on every fetch, then you can also specify git config fetch.prune true (enables for one repo only) or git config --global fetch.prune true (enables for all repos). Note that this might cause unpushed commits or branches to be removed.