Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 757
JabberwockyDecompiler Avatar asked Nov 16 '15 15:11

JabberwockyDecompiler


People also ask

How do I refresh a branch code in Visual Studio?

Solution. You just need to run git fetch --prune command in your solution directory using GIT Bash or Command Prompt. After running this command, you will find that deleted remote branches are no longer showing up in remotes/origin section in Visual Studio.

How do I sync a branch in Visual Studio?

For more information on Sync, see Use git fetch, pull, push and sync for version control in Visual Studio. Open the Team Explorer and open the Sync view. Then select the Pull link under Incoming Commits to pull remote changes and merge them into your local branch.


2 Answers

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 
like image 164
VonC Avatar answered Sep 30 '22 13:09

VonC


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"

like image 33
yaniv Avatar answered Sep 30 '22 14:09

yaniv