在使用 Git 时,如何刷新 VisualStudio 中的分支(本地/远程) ?

更新

升级到 Visual Studio 201916.8.X 有一个新的 git 工作流,其中还包括一个提取按钮。

enter image description here

摘要

我正在学习如何在 VisualStudio 中使用 Git。我最近执行了一个 pull 请求,其中功能部件分支在合并后被删除。在 VisualStudio 中,特性分支仍然出现在我的本地分支和远程分支中。我知道如何右键单击并删除分支,这是一个变通方法,因为团队中的其他人可能会在我不知情的情况下完成对分支的拉请求。如果他们事后删除分支,我不会知道他们这样做了。

提问

如何使用 Git 上的内容刷新 VisualStudio 分支?

我想要的

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?".

技术眼镜

我正在使用 Visual Studio 2015 Enterprise (对于低于16.8的版本,VS 2019似乎仍然是一个症状)

附加例子

这可能是另外一个问题,但是它非常适合这里。我只是注意到,如果我创建了一个分支(比如在一个 VM 中创建一个分支,然后用另一个 VM 查看同一个回购) ,Visual Studio 没有更新分支列表的 git fetch选项。顶部的刷新按钮似乎没有任何作用。只要我在 bash 中运行 git fetch,Visual Studio 就有了新的分支。我希望能有人来处理这个。

47668 次浏览

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

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.

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.

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"

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

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

enter image description here