This guide describes how to delete a branch using the Visual Studio Code (VSC) editor and GitHub.
Note: Take care when deleting a branch, and only delete a branch if you’re sure that you need to.
The options for deleting a branch described in this guide only work on branches that have been pushed to, and merged with, GitHub already. Forcing the deletion of unmerged branches is beyond the scope of this guide.
Information about the Git
delete branch
command is available from the Software Freedom Conservancy’s Git branch documentation.
Prerequisites
Before you proceed complete the following:
- Install Visual Studio Code
- Install Docs Authoring Pack
- Install Word Count extension
- Install Pull Requests extension
- Install Azure Repos extension
- Install Git client
- Set Git credentials
- Terminology and concepts
- General project workflow
- Download course files (clone repo)
- Project branching policies
- Create new branch in VSC
- Send (push) files
Topics in this guide
Delete a branch using VSC
Complete the following steps to delete a branch using VSC.
Note: To complete this guide you must know how to switch branches, as described in the guide Switch branches.
-
Open VSC, and choose File > Open folder from the top menu.
-
Use the VSC open folder explorer to select the local repository folder (repo) on your computer, and then choose Select folder.
-
From the VSC top menu, choose Terminal > New Terminal.
Note: The VSC status bar, at the bottom of the editor window, indicates the name of the branch that VSC is currently switched to. For example, in the previous image, the VSC status bar indicates that VSC is currently switched to the branch called master.
You can’t delete the branch that VSC (or Git) is currently switched to. Switch to a branch other than the branch you want to delete, and then continue following this guide.
-
In the VSC Terminal, type the following command to delete the branch from your local repo, and then press Enter.
For example, in the following image, the local branch old-branch will be deleted. Replace branch_name with the name of the local branch you want to delete.
git branch --delete <branch_name>
A message in the VSC Terminal confirms that the branch is deleted from your local repo.
-
Delete the branch from GitHub with the
git push
command by typing the following command into the VSC Terminal, and then press Enter.git push origin --delete <branch_name>
For example, in the following image, the GitHub branch old-branch will be deleted from GitHub. Replace branch_name with the name of the GitHub branch you want to delete.
A message in the VSC Terminal confirms that the branch is deleted from GitHub.
You’ve deleted a branch using VSC successfully.
Delete a branch using GitHub
Complete the following steps to delete a branch using GitHub.
Note: Deleting a branch using GitHub removes the branch from GitHub only. Your local repo might retain a copy of the branch until you pull updates from GitHub into your local repo, or clone the GitHub repo again. To delete a branch from your local repo and from GitHub, follow the steps in Delete a branch using VSC.
-
Open a web browser, go to the URL for the project’s GitHub repo, and sign in to GitHub.
-
Select the Branches icon.
-
Select the trash can icon beside the branch you want delete (on the right side).
For example, in the following image, the branch called old-branch will be deleted.
-
The message Deleted just now and Restore button, beside the branch name, indicate that the branch has been deleted from GitHub.
You’ve deleted a branch using GitHub successfully.
Appendices
Check the following supplementary Appendices for more details and context.