Last updated: Apr 6, 2024
Reading time·5 min
To delete a Git branch in VS Code:
Here is a short clip that demonstrates how to switch a branch.
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.Note that the currently active branch is not shown.
If you need to delete the current branch, you have to checkout another branch and then issue the Git: Delete Branch... command.
Here is a short clip that demonstrates how this works.
You can also use the Source Control View to delete a branch.
You can also focus the Source Control view with a keyboard shortcut:
Ctrl
+ Shift
+ G
(works on Windows, macOS and Linux)If you have deleted branches from GitHub, but they still show up in VS Code, you
have to use the git fetch --prune
command.
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.The command is used to clean outdated branches.
It connects to your remote repository and fetches all remote branch refs.
It then deletes remote refs that are no longer on the remote repository.
However, the command doesn't remove the corresponding local git branch.
If you want to remove a local git branch, you have to issue the Delete Branch command.
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.When you remove a branch from GitHub, the local branch doesn't automatically get removed.
The local copy of the branch remains on your machine and is visible in VS Code until you remove it as well.
There is a setting in VS Code that allows you to automatically run the Git fetch prune command when fetching remote refs.
Ctrl
+ Shift
+ P
(or Command
+ Shift
+ P
on macOS).F1
to open the Command Palette.You can also open the settings screen by pressing Ctrl
+ ,
on Windows and
Linux or Cmd
+ ,
on macOS.
If the Git: Prune on Fetch setting is checked, then the git fetch --prune
command is automatically run when fetching remote refs.
In other words, VS Code will automatically delete remote branches (e.g.
origin/xyz
).
However, note that the corresponding local branches are still not deleted automatically.
You can also use your terminal to delete git branches.
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.You can also open the terminal by using a keyboard shortcut:
Ctrl
+ ` (backtick).Ctrl
+ ` (backtick).git fetch --prune
command to delete Git branches that
have been removed from GitHub but are still shown locally in VS Code.git fetch --prune
You can use the git branch -d <branch-name>
command to delete a local Git
branch.
git branch -d <your-branch>
Make sure to replace the placeholder with your actual branch name, e.g.
git branch -d dev
.
You can also use the -D
option if you want to force delete a branch.
git branch -D <your-branch>
I've also written a detailed guide on how to undo the last git commit in VS Code.
If you need to configure VS Code as your default Git editor, difftool and mergetool, click on the link and follow the instructions.
You can learn more about the related topics by checking out the following tutorials: