Git remote branch deleted, but still it appears in 'branch -a'

GitGit Branch

Git Problem Overview


Let's say I had a branch named coolbranch in my repository.

Now, I decided to delete it (both remotely and locally) with:

git push origin :coolbranch
git branch -D coolbranch

Great! Now the branch is really deleted.

But when I run

git branch -a

I still get:

remotes/origin/coolbranch

Something to notice, is that when I clone a new repository, everything is fine and git branch -a doesn't show the branch.

I want to know - is there a way to delete the branch from the branch -a list without cloning a new instance?

Git Solutions


Solution 1 - Git

git remote prune origin, as suggested in the other answer, will remove all such stale branches. That's probably what you'd want in most cases, but if you want to just remove that particular remote-tracking branch, you should do:

git branch -d -r origin/coolbranch

(The -r is easy to forget...)

-r in this case will "List or delete (if used with -d) the remote-tracking branches." according to the Git documentation found here: https://git-scm.com/docs/git-branch

Solution 2 - Git

Try:

git remote prune origin

From the Git remote documentation:

prune

> 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.

Solution 3 - Git

Don't forget the awesome

git fetch -p

which fetches and prunes all origins.

Solution 4 - Git

In our particular case, we use Stash as our remote Git repository. We tried all the previous answers and nothing was working. We ended up having to do the following:

git branch –D branch-name (delete from local)
git push origin :branch-name (delete from remote)

Then when users went to pull changes, they needed to do the following:

git fetch -p

Solution 5 - Git

Use:

git remote prune <remote>

Where <remote> is a remote source name like origin or upstream.

Example: git remote prune origin

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
QuestionyonixView Question on Stackoverflow
Solution 1 - GitMark LongairView Answer on Stackoverflow
Solution 2 - GitBrian PhillipsView Answer on Stackoverflow
Solution 3 - GitoripView Answer on Stackoverflow
Solution 4 - GitShadowvailView Answer on Stackoverflow
Solution 5 - Gitlikaiguo.happyView Answer on Stackoverflow