Visual Studio Code - remove branches deleted on GitHub that still show in VS Code?

Visual StudioGithubVisual Studio-Code

Visual Studio Problem Overview


In VSCode, after I do a pull request and delete the branch on GitHub, that branch still shows up in Visual Studio Code. If I select the branch, it gives an Error, as expected.

How do I remove these now deleted branches from VSCode - can I do it automatically?

Visual Studio Solutions


Solution 1 - Visual Studio

Apparently, this feature is intentional. I found out that a correct way to remove all remote branches that have been deleted from Github is to run the following command.

git fetch --prune

Then restart visual studio to remove the branches from the command palette

Solution 2 - Visual Studio

Local branches can be removed from Visual Studio Code by opening the Command Pallete (Ctrl-Shift-P) then Selecting Git: Delete Branch..., you can then delete the local branch by selecting the appropriate one from the list.

Solution 3 - Visual Studio

Open the command palette (Ctrl+Shift+P) and select Git: Fetch (Prune).

This feature was merged into VS Code on Nov 20, 2018.

Solution 4 - Visual Studio

Branches removed from GitHub are well... just removed from GitHub. You still have local copy of branch on your machine. To delete local branch run git branch -d the_local_branch. There is no command in VS Code to do so, but you can start terminal in VSCode using View: Toggle Integrated Terminal command and run command from it.

For more information about branch management please visit git documentation - https://git-scm.com/book/be/v2/Git-Branching-Branch-Management

Solution 5 - Visual Studio

All you need to do is to run this command:

git remote prune origin

Something extra that you can do, because it's annoying sometimes to open a terminal just for that.. you can add a task in vscode.

To do that please make follow these steps:

  1. In VSCode View > Command Palette (cmd/ctrl + Shift + P)
  2. type Configure Task
  3. Select Create tasks.json file from template and a new file will be created under .vscode folder.
  4. Inside tasks array add this:

{ "label": "Git Prune", "type": "shell", "command": "git remote prune origin", "problemMatcher": [] }

How to use it:

  1. Open Command Palette
  2. Type Run Task and select it
  3. Select Git Prune

Reference:

  1. Git prune

Solution 6 - Visual Studio

I interpreted the question to be: how can I delete my local branches that have been merged, since I've been using Git Fetch (Prune) from the command palette. This may be considered a "hack", but it's what I use. In the PowerShell terminal:

$branches = (git branch --merged).replace(" ", "").replace("*", "") | ? { $_ -ne "develop" -and $_ -ne "master" }
foreach ($branch in $branches) { git branch $branch -d }

In case you're not familiar with PoSH, here's what that does: the first line gets the name of all merged branches (with the exception of develop and master), and the second line loops through that list and runs "git branch -d". As long as the branch is merged completely, you should see:

Deleted branch <branch name> (was <commit ID>).

for each branch. Occasionally I'll run into a branch that fails to be deleted - if this happens, and you're sure that it's safe to be deleted (i.e. you won't lose local work that hasn't been stored), you can run:

git branch <branch name> -D

Note the capital D - that forcibly deletes the local branch.

Solution 7 - Visual Studio

You don't have to git fetch --prune and restart VSCode anymore.
With VSCode 1.52 (Nov. 2020), you now have:

> ## Git: Prune on fetch > > Enabling the git.pruneOnFetch setting will make VS Code run git fetch --prune when fetching remote refs.

No more extra branch locally once they have been deleted from GitHub.

See PR 89249, fixing issue 86813:

>Usage: > >yaml >{ > "git.pruneOnFetch": true >} > > > Setting is false by default. > > This would add the --prune flag to all git fetches.

Solution 8 - Visual Studio

I found a way to fix this. So you need to remove the remote that links to the Github repo, then add the remote again.

All the branches that are deleted from Github will no longer show up in vscode. Assuming that origin is the name for the remote repo.

git remote remove origin

Then

git remote add origin git@github.com:your-username/repo-name.git

Solution 9 - Visual Studio

You can remove all the local branches (except master) with:

git branch -r | grep -v "master" | xargs git branch -D

And you can remove all the branches still appearing as origin/XXXX in VSCode but already deleted in origin with:

git fetch --prune

Note:

The first command above (taken from https://stackoverflow.com/a/52006270/3120163):

  • list all the branches
  • remove the master branch from the list
  • delete the branches in the list

Solution 10 - Visual Studio

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

Solution 11 - Visual Studio

The shorter command is:

git fetch -p

Solution 12 - Visual Studio

this method by davidhu works which is below with a little touch.

This command remove branches deleted on Github that still show in Vs code. let assume what is inside the braces [origin] is the name of the remote repo.

git remote remove origin

Then

git remote add origin [email protected]:your-username/repo-name.git

or you can removed it manually from Vs code

  1. click the source control icon found in the left side of Vs code

  2. click on the more icon or more action, "then click on "Romote" it will show you to either "add remote " or "remove romote"

  3. click on "remove romote", it will show you list of all remote on you Vs code, you can then pick the one you want to remove and press "Enter"

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
QuestionDrenaiView Question on Stackoverflow
Solution 1 - Visual StudiodavidhuView Answer on Stackoverflow
Solution 2 - Visual StudioMatthew Disney-CookView Answer on Stackoverflow
Solution 3 - Visual Studiotgr42View Answer on Stackoverflow
Solution 4 - Visual StudioKrzysztof CieslakView Answer on Stackoverflow
Solution 5 - Visual StudioBogdan Alexandru MilitaruView Answer on Stackoverflow
Solution 6 - Visual StudioDarren GView Answer on Stackoverflow
Solution 7 - Visual StudioVonCView Answer on Stackoverflow
Solution 8 - Visual StudiodavidhuView Answer on Stackoverflow
Solution 9 - Visual StudioAdrien RenaudView Answer on Stackoverflow
Solution 10 - Visual StudioBashir MomenView Answer on Stackoverflow
Solution 11 - Visual StudioAl GolView Answer on Stackoverflow
Solution 12 - Visual StudioRaySunView Answer on Stackoverflow