How do I refresh branches (local/remote) in Visual Studio when using Git?

GitVisual StudioVisual Studio-2019

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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"

Solution 3 - Git

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.

Solution 4 - Git

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

enter image description here

Solution 5 - Git

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

Solution 6 - Git

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.

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
QuestionJabberwockyDecompilerView Question on Stackoverflow
Solution 1 - GitVonCView Answer on Stackoverflow
Solution 2 - GityanivView Answer on Stackoverflow
Solution 3 - GitThaodenView Answer on Stackoverflow
Solution 4 - GitStephen McDowellView Answer on Stackoverflow
Solution 5 - GitMark ArnoldView Answer on Stackoverflow
Solution 6 - Gitlkn2993View Answer on Stackoverflow