Drenai
Drenai

Reputation: 12357

How to remove branches already deleted on GitHub that still show up in VS Code?

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?

Upvotes: 246

Views: 239724

Answers (20)

Martin Uildriks
Martin Uildriks

Reputation: 71

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

Upvotes: 3

Derek Swenningsen
Derek Swenningsen

Reputation: 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!

Upvotes: -1

Jacob Joy
Jacob Joy

Reputation: 556

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

Upvotes: 0

julien padovani
julien padovani

Reputation: 11

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

Upvotes: 1

Drazen Bjelovuk
Drazen Bjelovuk

Reputation: 5472

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

Upvotes: 7

RaySun
RaySun

Reputation: 239

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"

Upvotes: 1

Arvind
Arvind

Reputation: 49

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.

Upvotes: -2

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

Upvotes: 15

tgr42
tgr42

Reputation: 930

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.

Upvotes: 46

Matthew Disney-Cook
Matthew Disney-Cook

Reputation: 1405

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.

Upvotes: 138

saran3h
saran3h

Reputation: 14022

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

Upvotes: 1

hdsenevi
hdsenevi

Reputation: 981

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

Upvotes: -2

Bashir Momen
Bashir Momen

Reputation: 1681

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

Upvotes: 9

VonC
VonC

Reputation: 1324547

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.

Upvotes: 19

Adrien Renaud
Adrien Renaud

Reputation: 2787

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

Upvotes: 5

Darren G
Darren G

Reputation: 191

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.

Upvotes: 12

Al Gol
Al Gol

Reputation: 47

The shorter command is:

git fetch -p

Upvotes: 2

davidhu
davidhu

Reputation: 10432

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

Upvotes: 371

davidhu
davidhu

Reputation: 10432

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

Upvotes: 6

Krzysztof Cieslak
Krzysztof Cieslak

Reputation: 1735

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

Upvotes: 23

Related Questions