Reputation: 12357
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
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.
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
Upvotes: 3
Reputation: 1
First I ran...
git show-branch
This showed me branches available for deletion...
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
Reputation: 556
git fetch --prune
git branch -vv | grep ': gone]' | awk '{print $1}' | xargs -n 1 git branch -D
Upvotes: 0
Reputation: 11
Two steps required (I found the answers above and merged them)
Delete remote branches from VS Code that have been deleted from GitHub
git fetch --prune
Delete local branches from VS code (for the branch name don't enter 'origin/')
git branch --delete branchname
Upvotes: 1
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...
Upvotes: 7
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:
Upvotes: 1
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
Reputation: 1210
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:
{
"label": "Git Prune",
"type": "shell",
"command": "git remote prune origin",
"problemMatcher": []
}
How to use it:
Reference:
Upvotes: 15
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
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
Reputation: 14022
There are two types of branches which will be available in your local machine.
git branch -a
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
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
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:
Upvotes: 9
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 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.
Upvotes: 19
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):
Upvotes: 5
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
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
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
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