How to easily delete Git Branches in Visual Studio Code | bobbyhadz (2024)

How to easily delete Git Branches in Visual Studio Code

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

How to easily delete Git Branches in Visual Studio Code | bobbyhadz (1)

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

How to easily delete Git Branches in Visual Studio Code | bobbyhadz (2)

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

How to easily delete Git Branches in Visual Studio Code | bobbyhadz (3)

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

How to easily delete Git Branches in Visual Studio Code | bobbyhadz (4)

Note that the currently active branch is not shown.

If you need to delete the current branch, you have to checkout another branchand then issue the Git: Delete Branch... command.

Here is a short clip that demonstrates how this works.

How to easily delete Git Branches in Visual Studio Code | bobbyhadz (5)

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

How to easily delete Git Branches in Visual Studio Code | bobbyhadz (6)

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

How to easily delete Git Branches in Visual Studio Code | bobbyhadz (7)

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 selectCheckout to if you have to switch to a different branch.

How to easily delete Git Branches in Visual Studio Code | bobbyhadz (8)

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

How to easily delete Git Branches in Visual Studio Code | bobbyhadz (9)

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

How to easily delete Git Branches in Visual Studio Code | bobbyhadz (10)

# 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, youhave 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).

How to easily delete Git Branches in Visual Studio Code | bobbyhadz (11)

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

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

How to easily delete Git Branches in Visual Studio Code | bobbyhadz (12)

When you remove a branch from GitHub, the local branch doesn't automatically getremoved.

The local copy of the branch remains on your machine and is visible in VS Codeuntil you remove it as well.

There is a setting in VS Code that allows you to automatically run the Git fetchprune 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.

How to easily delete Git Branches in Visual Studio Code | bobbyhadz (13)

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

  1. Type git prune` in the search field.

How to easily delete Git Branches in Visual Studio Code | bobbyhadz (14)

If the Git: Prune on Fetch setting is checked, then the git fetch --prunecommand 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 deletedautomatically.

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

How to easily delete Git Branches in Visual Studio Code | bobbyhadz (15)

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 thathave been removed from GitHub but are still shown locally in VS Code.

shell

Copied!

git fetch --prune

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

shell

Copied!

git branch -d <your-branch>

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

How to easily delete Git Branches in Visual Studio Code | bobbyhadz (16)

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

shell

Copied!

git branch -D <your-branch>

I've also written a detailed guide onhow to undo the last git commit in VS Code.

If you need to configureVS 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 followingtutorials:

  • Changing the Git user or the GitHub account in VS Code
  • What is the .vscode folder and should you Commit it to Git
  • VS Code: Git not found. Install it or configure it using the 'git.path' setting
  • How to view the Git History in Visual Studio Code
  • Please clean your repository working tree before checkout
  • How to rename a Directory or a File on the GitHub website
  • How to highlight modified lines in Visual Studio Code
  • Unable to load schema from vscode://schemas/settings/folder
How to easily delete Git Branches in Visual Studio Code | bobbyhadz (2024)

References

Top Articles
Latest Posts
Article information

Author: Nathanial Hackett

Last Updated:

Views: 5856

Rating: 4.1 / 5 (52 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Nathanial Hackett

Birthday: 1997-10-09

Address: Apt. 935 264 Abshire Canyon, South Nerissachester, NM 01800

Phone: +9752624861224

Job: Forward Technology Assistant

Hobby: Listening to music, Shopping, Vacation, Baton twirling, Flower arranging, Blacksmithing, Do it yourself

Introduction: My name is Nathanial Hackett, I am a lovely, curious, smiling, lively, thoughtful, courageous, lively person who loves writing and wants to share my knowledge and understanding with you.