How to easily delete Git Branches in Visual Studio Code
# Table of Contents
- How to easily delete Git Branches in Visual Studio Code
- Delete a Git Branch in VS Code using the Source Control View
- Deleting Git branches that have been deleted from GitHub but still show in VS Code
- 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:
- Make sure the branch you want to delete is not currently active.
Here is a short clip that demonstrates how to switch a branch.
- 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.
- Type Delete branch and select Git: Delete Branch....
- The next screen prompts you to select the branch you want to delete.
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.
- You can click on the current branch label at the left bottom corner to verifythe branch has been 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.
- Click on the Source Control icon in the Activity Bar on the left.
You can also focus the Source Control view with a keyboard shortcut:
Ctrl
+Shift
+G
(works on Windows, macOS and Linux)
- Click on the three dots icon (ellipsis) in the top right corner and selectCheckout to if you have to switch to a different branch.
- Then click on the three dots icon in the top right corner again, hover overBranch and select Delete Branch....
- Select which branch you want to delete from the 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, youhave to use the git fetch --prune
command.
- Press:
Ctrl
+Shift
+P
on Windows and Linux.Command
+Shift
+P
on macOS.
Note: you can also press F1
to open the Command Palette.
- Type git fetch prune and select 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 Branchcommand.
- Press:
Ctrl
+Shift
+P
on Windows and Linux.Command
+Shift
+P
on macOS.
Note: you can also press F1
to open the Command Palette.
- Type Delete branch and select Git: Delete Branch....
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.
- Press
Ctrl
+Shift
+P
(orCommand
+Shift
+P
on macOS).
Note: you can also press F1
to open the Command Palette.
- Type user settings and select Preferences: Open User Settings.
You can also open the settings screen by pressing Ctrl
+ ,
on Windows andLinux or Cmd
+ ,
on macOS.
- Type git prune` in the search field.
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 deletedautomatically.
# Deleting Git branches from within your Terminal in VS Code
You can also use your terminal to delete git branches.
- Press:
Ctrl
+Shift
+P
on Windows and Linux.Command
+Shift
+P
on macOS.
Note: you can also press F1
to open the Command Palette.
- Type toggle terminal and select View: Toggle Terminal.
You can also open the terminal by using a keyboard shortcut:
- on Windows and Linux:
Ctrl
+ ` (backtick). - on macOS:
Ctrl
+ ` (backtick).
- 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
.
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