200

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?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Drenai
  • 11,315
  • 9
  • 48
  • 82
  • 9
    To follow up (3yrs later), the correct answer for me is a combination of `git fetch --prune` to update/delete remote branch references, and `git branch -d ` to delete the local branch (`-D` to force). Restart VSCode refreshes palette – Drenai Feb 05 '20 at 00:01
  • No need to restart VS Code, just hit the Refresh button in the Source Control menu – A-S May 03 '20 at 00:24
  • I actually build a function to handle this and write a little article about it. [Link](https://medium.com/@davidhu314/my-favorite-git-alias-to-keep-my-branches-in-check-e0b49b7cbfd5) – davidhu Jul 07 '20 at 22:38
  • @Drenai why need to user `git branch -d `, for deleting individual branches one by one? – Aashiq Sep 09 '21 at 08:10
  • 1
    vs code has setting to always [prune branches during fetch](https://stackoverflow.com/a/65187314/6908282) - `git.pruneOnFetch` – Gangula Mar 13 '23 at 18:51
  • @Gangula Yes, I mention it [on this page](https://stackoverflow.com/a/65187314/6309), with VSCode 1.52+ (Nov. 2020). – VonC Mar 13 '23 at 19:15

15 Answers15

315

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

David Wilton
  • 344
  • 2
  • 11
davidhu
  • 9,523
  • 6
  • 32
  • 53
  • 25
    @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. – davidhu Nov 12 '17 at 23:37
  • 1
    @Brian I found that this would clear the remote deleted branches after a restart of VS Code – David Wilton Dec 07 '17 at 01:37
  • 9
    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 – Present practice Nov 13 '18 at 17:56
  • @AnnaOlshevskaia `git branch` only list the local branches, this command is supposed to remove the `remotes/branch_name` branches. Try `git branch -a` to see everything. – davidhu Nov 13 '18 at 20:26
  • 4
    No need to restart VS Code, just hit the Refresh button in the Source Control menu – A-S May 03 '20 at 00:23
  • 6
    To run this in VS Code, use `Ctrl+Shift+P` and type `Git: Fetch (Prune)` which should do the same thing. – Eric Hedengren Jul 05 '20 at 20:24
  • I can still see the local branches after trying all these methods, what to do , only way is to delete one by one?, i have like 10 not updated branches – Aashiq Sep 09 '21 at 08:14
  • 10
    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. – davidhu Sep 23 '21 at 13:52
  • This answer actually only makes it even harder for you. You still have to delete all the local branches one by one. And since all the remote branches have been removed by the command, there is a dialog `The branch '...' is not fully merged. Delete anyway?`, which you have to accept for every delete. – Tigran Jan 09 '23 at 11:22
  • Why does this answer have 300 upvotes? – Pieterjan Apr 12 '23 at 16:41
  • This won't remove local branches which is the main ask. I wonder why is it getting so many upvotes. It's a basic git command to sync "remote" branches but has nothing to do with local branches. – saran3h Jul 13 '23 at 05:14
122

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.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Matthew Disney-Cook
  • 1,245
  • 1
  • 8
  • 3
  • 2
    Haven't test it through, but it seems to only show local branches, so it should work as described. – jave.web Nov 07 '18 at 18:42
  • 2
    @TylerBell As he already says, it is the method of removing the local branch not remote branch. – Anbuselvan Rocky May 26 '19 at 09:51
  • 3
    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 – Serzhan Akhmetov Jul 29 '19 at 09:53
  • 1
    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". – Shai Petel Jul 27 '21 at 18:50
33

Open the command palette (Ctrl+Shift+P on Windows/Linux, CMD+Shift+P on Mac) and select Git: Fetch (Prune).

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

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
tgr42
  • 790
  • 8
  • 11
21

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

Krzysztof Cieslak
  • 1,695
  • 13
  • 14
14

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    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 Aug 24 '22 at 22:41
  • 2
    @ekkis [`git prune`](https://stackoverflow.com/a/36083638/6309) would remove your local "remote tracking branches", not your local branches. It removes `origin/xxx`, if `xxx` no longer exists in the remote repository. – VonC Aug 25 '22 at 05:31
  • 1
    This should probably be considered as the answer, since the question is targeted to VS Code and not Git in general – Gangula Nov 12 '22 at 10:06
11

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
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • 1
    Do you know if `git remote prune origin` differs from the accepted answer of `git fetch --prune`? The Run Task detail is great, thanks for posting it – Drenai Sep 10 '18 at 11:44
  • @Drenai No, them are the same. But for a full response look [here](https://stackoverflow.com/questions/20106712/what-are-the-differences-between-git-remote-prune-git-prune-git-fetch-prune#answer-20107184) – Bogdan Alexandru Militaru Sep 10 '18 at 12:04
11

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.

Darren G
  • 151
  • 1
  • 3
  • That is one crazy PowerShell command I have to give an upvote for bravery for running that on your repo – Drenai Mar 14 '19 at 22:15
  • 2
    There’s a fine line between genius and insanity. This is well away from that line, on the insanity side. – Darren G Mar 16 '19 at 01:51
  • 1
    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: https://stackoverflow.com/questions/6127328/how-can-i-delete-all-git-branches-which-have-been-merged – Scott Stafford Dec 02 '19 at 14:33
  • @ScottStafford For me a 170 character command to delete branches is on the complex side of the `simple | complex` divide – Drenai Dec 02 '19 at 20:56
7

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

Bashir Momen
  • 1,461
  • 19
  • 17
  • 1
    This is awesome! thank you for sharing probably the easiest method – d0rf47 Jun 16 '22 at 14:51
  • 3
    Just FYI - This answer is for Visual Studio, not Visual Studio Code. Same question is [asked here for Visual Studio](https://stackoverflow.com/a/44392375/6908282) – Gangula Nov 12 '22 at 10:08
3

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
davidhu
  • 9,523
  • 6
  • 32
  • 53
  • 1
    not that I know of. It's the only way I found that will remove the remote branches that no longer exists. – davidhu May 14 '17 at 18:03
3

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
Adrien Renaud
  • 2,439
  • 18
  • 22
1

The shorter command is:

git fetch -p
Al Gol
  • 37
  • 2
0

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 **git@github.com: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"
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
RaySun
  • 139
  • 1
  • 5
0

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

saran3h
  • 12,353
  • 4
  • 42
  • 54
  • Does that command delete all local branches including ones that haven't been pushed to the remote? – Drenai Jul 25 '23 at 07:41
  • 1
    @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. – saran3h Jul 25 '23 at 07:58
-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

hdsenevi
  • 953
  • 2
  • 14
  • 27
-1

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.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Arvind
  • 1
  • 1