When a colleague pushes a new branch to Github, we can run the following to get it locally:
git pull # or git fetch
git checkout <branch-name>
This works because git uses what’s called “remote references” to keep track of
the last known state of remote branches, which are essentially read-only
bookmarks. In this case, git pull would create a new remote reference for the
new remote branch apart from updating existing remote references. Then git
checkout ... would create a new local branch that tracks the new remote branch
and switch to it.
That’s all well and good until there are too many branches in the codebase,
which is not at all uncommon when working in a reasonably sized team. Git
automatically creates remote references for all known remote branches, but it
doesn’t automatically remove stale remote references when the remote branches
are deleted. This annoys me because the stale remote references might mess with
my auto-completion for branch names. After some Googling, I managed to find a
way to remove them for the default remote connection origin:
git remote prune origin
Also the following command lists remote references:
git remote show origin
After sharing my findings with my colleagues, they pointed out that passing the
--prune option to git pull or git fetch would do the trick as well. As
mentioned in this nice tutorial for git prune, the following:
git fetch --prune
is the same as:
git fetch --all && git remote prune
Share on
X Facebook LinkedIn BlueskyComments are configured with provider: disqus, but are disabled in non-production environments.