'Visual Studio Code - remove branches deleted on GitHub that still show in VS Code?
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?
Solution 1:[1]
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:[2]
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:[3]
Open the command palette (Ctrl+Shift+P) and select Git: Fetch (Prune).
Solution 4:[4]
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:[5]
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:
- In VSCode View > Command Palette (cmd/ctrl + Shift + P)
- type Configure Task
- Select Create tasks.json file from template and a new file will be created under .vscode folder.
- Inside tasks array add this:
{ "label": "Git Prune", "type": "shell", "command": "git remote prune origin", "problemMatcher": [] }
How to use it:
- Open Command Palette
- Type Run Task and select it
- Select Git Prune
Reference:
Solution 6:[6]
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:[7]
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 rungit 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:
{ "git.pruneOnFetch": true }
Setting is
false
by default.This would add the
--prune
flag to all git fetches.
Solution 8:[8]
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 [email protected]:your-username/repo-name.git
Solution 9:[9]
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:[10]
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:
Solution 11:[11]
The shorter command is:
git fetch -p
Solution 12:[12]
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
click the source control icon found in the left side of Vs code
click on the more icon or more action, "then click on "Romote" it will show you to either "add remote " or "remove romote"
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"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow