Delete a branch locally on your computer, then delete it on GitHub
You delete a GitHub branch in two separate steps: first on your local machine (the copy of the repository on your computer), then on GitHub's servers. Most of the time you'll do both, though you can do either one alone if you need to. Local deletion removes the branch from your computer. Remote deletion removes it from GitHub, so other people on your team won't see it anymore.
The reason for two steps is that your local copy and the GitHub copy are independent. Deleting one doesn't automatically delete the other. This matters because you might want to keep a branch locally while removing it from GitHub, or vice versa — though that's rare in practice.
Key Takeaways
- Delete a branch locally by running git branch -d branch-name in your terminal, where branch-name is the actual name of the branch you want to remove.
- Delete a branch on GitHub by going to the repository, clicking the Branches tab, finding the branch, and clicking the trash icon next to it.
- You cannot delete a branch you are currently on — switch to a different branch first, usually main or master.
- Use git branch -D (capital D) instead of lowercase -d if Git refuses to delete because the branch has unmerged changes you're sure you want to discard.
- After deleting locally, run git push origin --delete branch-name to remove it from GitHub in one command instead of using the web interface.
Delete a branch on your local machine
Open your terminal or command prompt and navigate to your repository folder. First, check which branch you're currently on by running git branch. You'll see a list of all your local branches with an asterisk next to the one you're on. If you're on the branch you want to delete, you must switch to a different branch first — usually git checkout main or git checkout master, depending on what your default branch is called.
Once you're on a different branch, run git branch -d branch-name, replacing branch-name with the actual name of the branch you want to delete. For example, if you want to delete a branch called "fix-login-bug", you would type git branch -d fix-login-bug. Git will confirm the deletion and the branch will be gone from your local machine.
If Git refuses and says the branch has unmerged changes, it's warning you that commits on this branch haven't been merged into another branch yet. If you're certain you want to delete it anyway, use git branch -D (capital D) instead. This forces the deletion without the safety check. Use this carefully — you're telling Git you know what you're doing and you don't want the warning.
Delete a branch on GitHub through the web interface
Go to your repository on GitHub and click the Branches tab near the top of the page. You'll see a list of all branches on GitHub. Find the branch you want to delete and look for a trash icon (usually on the right side of that row). Click it and GitHub will ask you to confirm. Click the confirmation button and the branch is deleted from GitHub.
This method is straightforward if you only need to delete one or two branches. If you're cleaning up many branches at once, the command-line method below is faster.
Delete a branch on GitHub from the command line
If you prefer to stay in your terminal, you can delete a branch on GitHub without opening the web interface. Run git push origin --delete branch-name, replacing branch-name with the name of the branch. For example: git push origin --delete fix-login-bug. This removes the branch from GitHub immediately.
This command works whether or not you've already deleted the branch locally. You can delete it remotely first, then locally, or both at once — the order doesn't matter. Many developers do both deletions in sequence: first the local deletion with git branch -d, then the remote deletion with git push origin --delete.
What happens after you delete a branch
Once a branch is deleted locally, it no longer appears when you run git branch. Once it's deleted on GitHub, other people on your team won't see it in the repository anymore. However, the commits that were on that branch are not deleted — they still exist in the repository's history, especially if they were merged into another branch like main. Deleting a branch only removes the label pointing to those commits, not the commits themselves.
If you accidentally delete a branch, you can usually recover it. GitHub keeps deleted branches in the repository for a limited time (typically 90 days). Contact your repository administrator or check GitHub's documentation on branch recovery if you need to restore one.
Common mistakes when deleting branches
The most common mistake is trying to delete a branch while you're on it. Git will refuse with an error message. The fix is simple: switch to main or master first with git checkout main, then delete the branch you wanted to remove.
Another mistake is deleting a branch that has unmerged commits and not realizing it. If you use git branch -D (capital D) to force deletion, those commits might be lost if they weren't merged anywhere. Before forcing a deletion, make sure the work on that branch has been merged into another branch or saved somewhere else.
A third mistake is deleting only the local copy or only the remote copy and forgetting the other. If you delete locally but not on GitHub, the branch still shows up for your teammates. If you delete on GitHub but not locally, it still takes up space on your computer. It's usually safest to delete both unless you have a specific reason not to.
Frequently Asked Questions
Can I delete the main or master branch?
GitHub prevents deletion of your default branch through the web interface as a safety measure. If you need to change which branch is the default, go to your repository settings and choose a different default branch first. You can then delete the old one if you want.
What if I delete a branch and then realize I needed it?
GitHub keeps deleted branches for about 90 days. Contact your repository owner or administrator to recover it. If the branch was already merged into main, the commits still exist there and you can create a new branch from those commits if needed.
Do I have to delete branches, or can I just leave them?
You don't have to delete branches, but it's good practice to clean them up after they're merged. Too many old branches make the repository harder to navigate and can slow down some operations. Most teams delete branches once the work is done and merged.
What's the difference between git branch -d and git branch -D?
The lowercase -d is safe — it refuses to delete if the branch has unmerged commits. The capital -D forces deletion without checking. Use -d by default and only use -D when you're absolutely certain you want to discard unmerged work.
Does deleting a branch delete the commits on it?
No. If the commits were merged into another branch, they stay in the repository history. If they weren't merged anywhere, they become orphaned but still exist for about 90 days before GitHub cleans them up. Deleting a branch only removes the branch label, not the commits themselves.