30

Visual Studio continues to show any branch I've pulled, even after that branch has been deleted (and I do not have it as the selected branch).

  • "git fetch --all --prune" does not remove them. git config
  • "remote.origin.prune true" (suggested here) does not remove them.

How can I remove the deleted branches from my system?

Update: I want an approach that determines what branches are gone and deletes them. If we select them specifically then sooner or later someone will delete a branch in use by mistake.

David Thielen
  • 28,723
  • 34
  • 119
  • 193
  • Has the branch been deleted locally, remotely, or both? On the off chance you have local copies still hanging around. – Chris Aug 27 '17 at 20:52

4 Answers4

34

I had remote branches that would not go away even after they were deleted on the server. This tip about setting "Prune remote branches" worked for me. I found it here.

Go to Team Explorer, and click Home button. Then Settings > Repository Settings, and set "Prune remote branches during fetch" drop-down to True. Don't forget to click "Update" button to save your edit.

After changing the setting I did a fetch on one of the deleted branches and all of the deleted branches disappeared.

Homer
  • 7,594
  • 14
  • 69
  • 109
12

In the newer version of visual studio 2019 for example 16.8.6 you need to go to settings and find git settings as shown below:

enter image description here

Bashir Momen
  • 1,461
  • 19
  • 17
9

prune is about removing any remote-tracking references that no longer exist on the remote.

So if the branch still exists on the remote side, it won't be deleted locally.

If the branch is deleted locally (with Visual Studio), its remote tracking instance will still be re-created at the next push.

You need to make sure the branch is also deleted on the remote side:

git push origin --delete abranch
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 3
    I don't want to delete a branch, I want to safely delete all local copies of branches that are deleted. And by safely, I want to do it in a way where I don't by mistake delete a branch that does still exist. – David Thielen Aug 28 '17 at 15:56
  • @DavidThielen then https://stackoverflow.com/a/41611648/6309 should work. – VonC Aug 28 '17 at 15:58
  • If you mean "git config --global fetch.prune true" it doesn't. I ran that followed by "git fetch --all --prune", exited and restarted Visual Studio - and deleted repositories I once opened are still listed. thanks - dave – David Thielen Aug 28 '17 at 16:33
  • @DavidThielen "deleted repositories"? I thought you want deleted branches removed. A branch is not a repository. – VonC Aug 28 '17 at 16:49
  • Sorry - deleted branches. I want to get rid of branches that no longer exist. – David Thielen Aug 28 '17 at 17:36
  • @DavidThielen If those branches no longer exist on the remote side, the `git fetch --prune --all` is supposed to delete them locally. Can you try a `git branch -avv` in the repo after said `git fetch --all --prune`? (in order to check with Git alone if this is working, before checking Visual Studio) – VonC Aug 28 '17 at 18:03
  • Hi; Sorry, didn't do it. Still lists branches that were deleted over a month ago. – David Thielen Aug 29 '17 at 20:39
  • 1
    @DavidThielen Sure. Now, can you try a `git branch -avv` in the repo after said `git fetch --all --prune`? – VonC Aug 29 '17 at 21:02
  • "git branch -avv" lists 5 branches as gone (they are) and 3 as behind (what does that mean?). So it tells me which ones are safe to delete. If that's the best that can be done, that works. thanks - dave – David Thielen Aug 30 '17 at 14:33
  • @DavidThielen At this point, I think we need the exact output (as well a the output of git remote -v): this should be cleaned up by prune. – VonC Aug 30 '17 at 15:25
  • @DavidThielen At least, it is working for some, according to this recent comment: https://stackoverflow.com/questions/33739400/how-do-i-refresh-branches-local-remote-in-visual-studio-when-using-git/33868140#comment78936110_33868140 – VonC Aug 31 '17 at 19:14
  • VisualStudio is a little flaky when it comes to Git so the answer I guess is this is the way to do it - but it doesn't work sometimes. Thanks for the help. – David Thielen Aug 31 '17 at 23:05
3

Visual Studio 2015 & 2017

  1. Open up Team Explorer and go to the Branches view.
  2. Locate the branch you want to delete. Make sure that you aren't checked out to that branch-you can't delete the branch you are currently working in.
  3. Right-click the branch name and select Delete. If you have unpublished changes, Visual Studio will ask and make sure you want to delete the branch so you don't possibly lose work Image
Rohit Poudel
  • 1,793
  • 2
  • 20
  • 24
  • 1
    This approach scares me because sooner or later someone will delete a branch that is still in use. Is there a way to figure out what branches I have locally that are now gone and delete those? – David Thielen Aug 28 '17 at 15:57
  • Hi i dont think that way....Point 2 clearly states that Make sure that you aren't checked out to that branch-you can't delete the branch you are currently working in.@David Thielen – Rohit Poudel Aug 29 '17 at 04:20
  • In your screenshot above you could delete the develop branch as it's not selected. But I'm guessing doing so would be very bad. – David Thielen Aug 30 '17 at 14:30
  • In the screen shot,only the bugfix i.e selected branch will be deleted.You can see by clicking on the image.@DavidThielen – Rohit Poudel Sep 11 '17 at 11:48
  • 2
    My point was you COULD also delete the develop branch this way. Therefore it's a dangerous approach as you can end up deleting a branch still in use. – David Thielen Sep 22 '17 at 16:30
  • 1
    Not a bad idea for a single user setup. Unfortunately, in VS2019 Comm. master branches are not displayed in the pane. You can display them by right click > View History where there are a marvelous bunch of options which I will probably never use. Case in point- it was a "simple" wish to edit an incorrect commit message just pushed, and somehow (maybe because can't push until the useless branch is fetched or pulled) ended up with two extra useless branches. We end up reeling from this ridiculous stunted git concept causing massive bloat to all the worlds repos. Rantovani al fine. – Laurie Stearn Aug 08 '20 at 16:10
  • 2
    Ehm, that last bit was brought on by a severe case of the late night GITters. – Laurie Stearn Aug 09 '20 at 07:51