255

In VS Code, 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 VS Code. Can it be done automatically?

5
  • No need to restart VS Code, just hit the Refresh button in the Source Control menu Commented May 3, 2020 at 0:24
  • I actually build a function to handle this and write a little article about it. Link Commented Jul 7, 2020 at 22:38
  • 1
    @Drenai why need to user git branch -d <branch-name>, for deleting individual branches one by one? Commented Sep 9, 2021 at 8:10
  • 1
    vs code has setting to always prune branches during fetch - git.pruneOnFetch Commented Mar 13, 2023 at 18:51
  • @Gangula Yes, I mention it on this page, with VSCode 1.52+ (Nov. 2020). Commented Mar 13, 2023 at 19:15

21 Answers 21

386

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

Sign up to request clarification or add additional context in comments.

13 Comments

@Brian, this does not remove any local branches you have. This command removes the origin/branch_name from the quick switch git menu on VSCode. For example, if you have a local branch test and push it to Github, there are two branches test, and origin/test on the git branch menu, the prune only removes the origin/test branch, not the test branch.
this command doesn't work. I still see all of the branches in the list both when do command git branch or click on the branch name to see a dropdown with all available branches
No need to restart VS Code, just hit the Refresh button in the Source Control menu
To run this in VS Code, use Ctrl+Shift+P and type Git: Fetch (Prune) which should do the same thing.
this is only meant to remove remote branches. to remove local branches, you can do git branch | xargs git branch -d. or use -D if you want to remove ones that are not merged also.
|
152

Local branches can be removed from Visual Studio Code by opening the Command Palette (Ctrl+Shift+P on Windows/Linux, CMD+Shift+P on Mac) then Selecting Git: Delete Branch.... You can then delete the local branch by selecting the appropriate one from the list.

5 Comments

Haven't test it through, but it seems to only show local branches, so it should work as described.
@TylerBell As he already says, it is the method of removing the local branch not remote branch.
this should have been the answer. i don't know why i was so stupid as to not know this myself. thank you @matthew-disney-cook
Nope, he clearly is talking about a remote branch. OP says after deleting it from a pull request, probably using the web UI or smth, the pull request is completed and the branch is removed (from the server), but still stays locally. So - clearly, he is talking about a synced branch on the server. So - OP was asking to remove the remote branch which shows up locally under "remote branches".
Should be the selected answer ! Thanks !
50

Delete remote branches (origin/branchname)

Open the command palette and select Git: Fetch (Prune).

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

Delete local branches (branchname)

VS Code doesn't currently have an easy way to delete all the local merged branches that accumulate via the PR process. There are several options:

  1. Manually delete each branch, e.g. via VS Code or command line (git branch and git branch -d branchname)

  2. Install git-removed-branches and run git removed-branches --prune (this is a third party tool, use at your own risk) - or roll your own script

  3. Go to VS Code issue 183906 and vote to get this feature added to VS Code.

3 Comments

is there a way to make this default behaviour of git pull in vscode ?
Yes, you can set git.pruneOnFetch: true
it still doesn't work
23

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

2 Comments

Quite dumb that they do not support native git commands within their git-command line. Badly designed.
Please see the above answer from @matthew-disney-cook
19

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:

{ "git.pruneOnFetch": true } 

Setting is false by default.

This would add the --prune flag to all git fetches.

3 Comments

I turned this setting on, clicked on Pull, Push/Fetch (prune) but I still see all the local branches for which there is no remote :(
@ekkis git prune would remove your local "remote tracking branches", not your local branches. It removes origin/xxx, if xxx no longer exists in the remote repository.
This should probably be considered as the answer, since the question is targeted to VS Code and not Git in general
15

All you need to do is to run this command:

git remote prune origin 

Something extra that you can do, since it's annoying sometimes to open a terminal just for that, is to make that a task in VS Code.

To do that please follow these steps:

  1. In VS Code 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 the tasks array, add this:
    { "label": "Git Prune", "type": "shell", "command": "git remote prune origin", "problemMatcher": [] } 

How to use it:

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

Reference:

  1. Git prune

1 Comment

@Drenai No, them are the same. But for a full response look here
12

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.

2 Comments

There’s a fine line between genius and insanity. This is well away from that line, on the insanity side.
No it's not. It's not that complex when you look at it, and anyway, it's just a powershell translation of answers from here: stackoverflow.com/questions/6127328/…
9

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

2 Comments

This is awesome! thank you for sharing probably the easiest method
Just FYI - This answer is for Visual Studio, not Visual Studio Code. Same question is asked here for Visual Studio
8

Looks like GitLens supports this with GitLens: Git Branch -> prune (deletes local branches with missing upstreams) or direct shortcut: GitLens: Git Prune Branches...

enter image description here

Comments

6

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 

1 Comment

not that I know of. It's the only way I found that will remove the remote branches that no longer exists.
5

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

1 Comment

I added an explanation @Drenai
3

This is an older question by now, but none of the answers I came across actually address the issue; everyone keeps going on about prune and fetch, but none of those actually remove the local branches from VSCode. Therefore I figured I'd post my solution.

I'm on Windows 11 running VSCode and typically work with Gitlab, but also Github. The command I'm running is based on the Linux equivalent and basically removes all local branches without upstream from the dropdown in VSCode.

dropdown with local branches

Note: be careful running this because you may end up deleting something you don't want to delete!

Linux: git branch --merged| grep -Ev "(^\*|main|master|develop|release)" | xargs git branch -d

Windows: git branch --merged | Where-Object {$_ -notmatch "(^\*|main|master|develop|release)"} | forEach-Object { & git branch -d $($_.Trim()) }

Explanation

  • First we collect all the branches that have been merged
  • Then we exclude branches that we don't want to delete such as main, master, develop, release--whatever branches you want to keep
  • Then we iterate over the remaining selection and delete each one

2 Comments

A similar answer was posted by saran3h
Similar, but not the same, plus I provide a solution for in Windows as well
2

The shorter command is:

git fetch -p 

Comments

2

Two steps required (I found the answers above and merged them)

  1. Delete remote branches from VS Code that have been deleted from GitHub

    git fetch --prune

  2. Delete local branches from VS code (for the branch name don't enter 'origin/')

    git branch --delete branchname

Comments

1

There are two types of branches which will be available in your local machine. git branch -a

  1. Local branches
  2. Remote branches

To ensure end to end clean up of all "stale" branches, you can run this little script:

git fetch -p | git branch --merged | egrep -v "(^\*|master|development)" | xargs git branch -d 

git fetch -p will remove all "remote" branches which are no longer available at the origin.

The rest of the command is not exactly what you are looking for but will still cover 99% cases. It will delete all "local" branches except master, development AND the branch on which you already are.

PS: If you are using windows, install xargs linux package for windows installed: https://github.com/bmatzelle/gow/releases/tag/v0.8.0

1 Comment

@Drenai no because there's git branch --merged in this command. If you remove it, it will consider all branches including those that are not pushed to remote.
1

This is an improvement on the answer by davidhu which works.

This command removes branches deleted on Github that still show in VS Code. Let's assume that 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 remove it manually from VS Code:

  1. Click the source control icon found on the left side of VS Code
  2. Click on the more icon or more action, then click on "Remote, which will show either "add remote " or "remove remote"
  3. Click on "remove remote", which will show you a list of all the remote repositories on your VS Code. You can then pick the one you want to remove and press "Enter"

Comments

0
git fetch --prune git branch -vv | grep ': gone]' | awk '{print $1}' | xargs -n 1 git branch -D 

Comments

0

In VSCODE search in the settings the word prune.
then enable the checkbox "Prune when fetching" for Git extension.

enter image description here

Comments

-1

This approach does not directly answer how to address the raised question in vscode. But it's still a valid way to get the same result.

I use git-removed-branches

Hope this helps someone

Comments

-1

First I ran...

git show-branch 

This showed me branches available for deletion...

Shows output of show-branch command

In this case, [demo] was already removed in GitHub, but was still showing up in VS Code.

Next I ran...

git branch -D demo 

And it removed it from VS Code.

Just replace demo with the name of your branch!

Comments

-2

In VS Code, just do

git branch -d <branch name> 

or

git branch -d <branch name> 

for unmerged branches.

It will forcefully remove branch names from VS Code.

1 Comment

Do you mean git branch -D in your second command?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.