88

Update

Upgrading to Visual Studio 2019 16.8.X has a new git workflow. This also includes a fetch button.

enter image description here

Summary

I am learning how to use Git with Visual Studio. I recently did a pull request where the feature branch was deleted after the merge. The feature branch still shows up in my local and remote branches in Visual Studio. I know how to right click and delete the branch, this is a workaround as others on the team may complete a pull request on a branch that I have without me knowing it. If they delete the branch afterwards I would not know they did so.

Question

How do you refresh Visual Studio branches with what is on Git?

What I expect

I would expect a button, link, or right click feature that on click checks for differences, if any are found it asks, "This branch no longer exists, would you like to remove it from Visual Studio?".

Tech Specs

I am using Visual Studio 2015 Enterprise (still seems to be a symptom of VS 2019 for versions lower than 16.8)

Additional Example

This could be another question, but it fits so well here. I just noticed that if I create a branch (say in one VM and look at the same repo with another), Visual Studio does not have a git fetch option to update the branch list. The refresh button at the top does not seem to do anything. As soon as I run git fetch in a bash, visual studio has the new branch. I would expect the refresh to take care of this.

JabberwockyDecompiler
  • 3,318
  • 2
  • 42
  • 54

6 Answers6

100

If the branch has been deleted on the server side, try in command line (since such a "button" doesn't seem to exist directly in Visual Studio):

git remote prune origin --dry-run

(remove the --dry-run option to actually delete the local branches)

Delete the corresponding local branch as well git branch -d aBranch.

Then restart your Visual Studio, and check it picks up the updated branch list. (comments mention you don't have to restart/refresh VS)

Note: I mentioned before in 2013 the configuration

git config remote.origin.prune true

That would automate that process, and seems to be supported by Visual Studio, as mentioned below by yaniv.


On VSCode, try also to activate the setting "Git: Prune on Fetch"

"git.pruneOnFetch": true
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 5
    There should be no need to restart - VS should detect the changes that you made to your refs and reload them. – Edward Thomson Nov 23 '15 at 18:01
  • 2
    This does update the remote branch, and the results do show in Visual Studio. For me to give you the bounty I would want to know if there is a button that is inside VS that executes the `prune` git command. – JabberwockyDecompiler Nov 23 '15 at 19:46
  • I can confirm that restart is not needed. Weird that the refresh button doesn't actually prune the branch itself. – David De Sloovere Jul 04 '16 at 09:01
  • @MamunReza OK. I have edited the answer accordingly. – VonC Dec 14 '16 at 05:32
  • Sorry, I'm an idiot. Where would one type this into? Is there a command line somewhere? – markthegrea Apr 13 '17 at 20:33
  • 1
    @markthegrea yes, in a command line, in your local repo: https://www.visualstudio.com/en-us/docs/git/command-prompt – VonC Apr 13 '17 at 20:56
  • After adding the prune true I went to Visual Studio and tried to fetch one of the remotes that I knew was gone and it updated the entire list of remotes to match the server. Thanks! – Glenn Aug 31 '17 at 18:34
  • This doesn't work for me - if I create a branch and I delete it from the server, then prune locally, my Visual Studio STILL has it. If I try to delete it within Visual Studio, it remains even after a reboot. – LordDelacroix Dec 01 '21 at 17:45
  • @LordDelacroix Can you try and activate the "Git: Prune on Fetch" setting on VSCode? – VonC Dec 01 '21 at 18:56
66

From : "Refresh git remote branches in Visual Studio"

You can configure git to do this automatically on fetch/pull running with this command:

git config remote.origin.prune true –global

Update:

Visual Studio 2017 version 15.7.3 and above you can do it using the UI :

  1. In Team Explorer , click the Home then Setting: enter image description here

  2. Select Global settings

  3. Change "Prune remote branches during fetch" to "True"

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
yaniv
  • 913
  • 8
  • 16
33

According to this blog post, you can set a git property via

git config remote.origin.prune true

that will remove deleted branches from your list when you perform a fetch.

Thaoden
  • 3,460
  • 3
  • 33
  • 45
21

For those looking for how to configuring pruning in VS2019 or VS2022 it has moved under the 'Git' menu.

enter image description here

Stephen McDowell
  • 839
  • 9
  • 21
13

VS 2017 appears to have support configurable in Team Explorer:

Team Explorer Home Settings Git > Global Settings Prune Remote Branches During Fetch: Unset, True, or False

Mark Arnold
  • 279
  • 2
  • 9
0

You should Unpublish the branch first, then others will notice that the branch is unpublished(by trying to pull the branch they will get an error), deleting the local branch is actually a separate process itself and must be done in order to get rid of the branch anyway.

lkn2993
  • 566
  • 7
  • 26
  • In the case of GitHub Pull Requests the branch can be deleted from the GitHub site. There is not an option to unpublish from that process. You do have an option to delete the branch at that point or delete the branch with an icon click. I also do not see an unpublish in Visual Studio. I am updating the question to include the version of VS I am using as I realized that I did not include that. – JabberwockyDecompiler Nov 23 '15 at 19:50