How to easily delete Git Branches in Visual Studio Code

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
5 min

banner

# Table of Contents

  1. How to easily delete Git Branches in Visual Studio Code
  2. Delete a Git Branch in VS Code using the Source Control View
  3. Deleting Git branches that have been deleted from GitHub but still show in VS Code
  4. Deleting Git branches from within your Terminal in VS Code

# How to easily delete Git Branches in Visual Studio Code

To delete a Git branch in VS Code:

  1. Make sure the branch you want to delete is not currently active.

click on label to switch branch

Here is a short clip that demonstrates how to switch a branch.

switch to a different branch

  1. Once the branch you want to delete is not active, press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type Delete branch and select Git: Delete Branch....

delete branch using command palette

  1. The next screen prompts you to select the branch you want to delete.

select branch to delete

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.

delete branch using command palette

  1. You can click on the current branch label at the left bottom corner to verify the branch has been deleted.

click to verify branch deleted

# Delete a Git Branch in VS Code using the Source Control View

You can also use the Source Control View to delete a branch.

  1. Click on the Source Control icon in the Activity Bar on the left.

click source control icon

You can also focus the Source Control view with a keyboard shortcut:

  • Ctrl + Shift + G (works on Windows, macOS and Linux)
  1. Click on the three dots icon (ellipsis) in the top right corner and select Checkout to if you have to switch to a different branch.

switch to different branch

  1. Then click on the three dots icon in the top right corner again, hover over Branch and select Delete Branch....

delete branch using source control

  1. Select which branch you want to delete from the list.

select branch to delete from list

# Deleting Git branches that have been deleted from GitHub but still show in VS Code

If you have deleted branches from GitHub, but they still show up in VS Code, you have to use the git fetch --prune command.

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type git fetch prune and select Git Fetch (Prune).

git fetch prune

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.

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type Delete branch and select Git: Delete Branch....

delete branch using 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.

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings and select Preferences: Open User Settings.

open user settings

You can also open the settings screen by pressing Ctrl + , on Windows and Linux or Cmd + , on macOS.

  1. Type git prune` in the search field.

search git prune

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.

# Deleting Git branches from within your Terminal in VS Code

You can also use your terminal to delete git branches.

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type toggle terminal and select View: Toggle Terminal.

vscode open terminal

You can also open the terminal by using a keyboard shortcut:

  • on Windows and Linux: Ctrl + ` (backtick).
  • on macOS: Ctrl + ` (backtick).
  1. You can issue the git fetch --prune command to delete Git branches that have been removed from GitHub but are still shown locally in VS Code.
shell
git fetch --prune

You can use the git branch -d <branch-name> command to delete a local Git branch.

shell
git branch -d <your-branch>

Make sure to replace the placeholder with your actual branch name, e.g. git branch -d dev.

delete branch using terminal

You can also use the -D option if you want to force delete a branch.

shell
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.