How to Delete a GitHub Branch (Local and Remote Guide)
Deleting a branch in GitHub is a common task when cleaning up merged or outdated code. This guide shows you how to remove both local and remote branches step by step.
Table of contents
How to Delete a GitHub Branch?
1. Delete a Local Branch
Follow these steps in your Git terminal:
- Open your terminal or command prompt.
- Navigate to your repository folder.
- Delete a merged branch by running
git branch -d branch-name
- Force delete an unmerged branch by running
git branch -D branch-name
This removes the branch only from your local machine.
For more basic Git operations, check out this guide on how to clone a repository in GitHub. If you’re interested in repository organization alongside branch cleanup, see this guide on how to create a folder in GitHub.
2. Delete a Remote Branch
To delete from GitHub’s remote repository:
- Make sure you pushed or merged changes if needed.
- Delete the branch remotely by running
git push origin --delete branch-name
- Alternatively, use the following command to remove it:
git push origin :branch-name
This ensures the branch is removed from GitHub so teammates cannot access it.
Need a quick backup of a single directory before removing the branch? Learn how to download a folder from GitHub and save only the files you need.
3. Delete a Branch in GitHub Web Interface
If you prefer using GitHub’s site:
- Go to your repository on GitHub.
- Click Branches in the top menu.
- Locate the branch you want to remove.
- Click the trash icon next to it.
The branch will be deleted immediately.
If you also want to share your work online, here’s a guide on how to host files on GitHub.
FAQs
Go to the Branch menu, select the branch, and click Delete.
Yes. If it had recent commits, you can restore it from the commit history.
No. Commits remain in the repository’s history even after branch deletion.
No. The default branch (main or master) cannot be deleted unless you change the default branch first.
Conclusion
Deleting branches in GitHub helps keep your repository clean and manageable. Use the command line or GitHub’s interface to remove both local and remote branches when they are no longer needed.
After removing outdated branches, it is equally important to understand how to merge branches in GitHub so you can keep your project streamlined and ensure changes flow back into the main branch smoothly.
To take your coding further, you might also want to explore using GitHub Copilot in Visual Studio.
Read our disclosure page to find out how can you help Windows Report sustain the editorial team. Read more
User forum
0 messages