36

I have a remote repository and 2 clones.
I create a branch in one of the clones e.g. test. I do some work and 2 commits. I merge to master branch and push -u the branch.
I do a git pull in the other clone.
I see both master and test.
In the first clone project I do:
git origin :test to delete test branch on remote repository.
test is deleted on remote repos.
I do git branch -D test and the test branch is deleted locally as well.
If I do git branch -a I get:

*master  
remotes/origin/master    

Now in the second repository I do a git pull.
On the pull the local test seems to be deleted but git seems to "think" that the remote test branch still exist.
If I do git branch -a I get:

* master  
  remotes/origin/HEAD -> origin/master  
  remotes/origin/master  
  remotes/origin/test    

Why does the deleted test branch appear as a remote branch?

Cratylus
  • 52,998
  • 69
  • 209
  • 339

2 Answers2

63

The default options for git fetch (and consequently git pull) do not prune deleted remote branches. I'm not sure what the logic behind this default is. In any case, to prune deleted remote branches, either fetch with

git fetch -p

or run

git remote prune [-n] <name>

explicitly. With the -n flag, it will report which branches will be pruned, but not actually prune them. See git-fetch(1) and git-remote(1) for more information.

Peter Lundgren
  • 8,787
  • 2
  • 26
  • 21
  • But if the repository was not mine how would I know that I would need to `prune`? – Cratylus Jun 16 '13 at 08:58
  • 1
    You can run `git remote prune -n ` to get a list, but I've resorted to just always running `git fetch -p` every time that I fetch. I didn't realize there wasn't an option for it on `git pull` since I never use `git pull`. – Peter Lundgren Jun 16 '13 at 14:21
  • How is it different from doing just `git branch -d` on local? I have a branch named "feature-test" in my local while it's been merged and deleted on the remote, now I did `git pull -p` but it's still there on my local repository. So pruning is not working?! – aderchox Jul 17 '19 at 17:23
  • Thanks @PeterLundgren. This helped me!. Github Pull Request does make the workflow easier can lead to quite some remote and local disconnected branches if one is not carefull. The two commands `git prune` and `git fetch -p` are added to my toolkit. – theking2 Mar 23 '22 at 13:24
13

Try using this command git remote prune origin. The deleted branch should disappear. This should remove local references to remote branches.

crea1
  • 11,077
  • 3
  • 36
  • 46
  • Actually I was expecting `git` to prompt me to do this the same way it prompts to do a `git pull`. Why doesn't it prompt?I have seen it prompting but perhaps it was in a different use case? – Cratylus Jun 15 '13 at 22:43
  • Yeah, I know what you mean. I can't really say i have seen git prompt anything about that when doing a pull, so i'm guessing it was a different use case. Tried to push something to a deleted branch perhaps? – crea1 Jun 15 '13 at 22:56