Delete a branch through the GitHub website

The fastest way to delete a branch is through GitHub's web interface. Open your repository, click the Branches tab near the top of the page, find the branch you want to remove, and click the trash icon next to it. GitHub will ask you to confirm — click the red Delete button. The branch is gone immediately.

This method works for any branch except your default branch (usually main or master). GitHub prevents you from deleting the default branch to protect against accidental loss. If you need to delete your default branch, you must first change which branch is set as default in your repository settings.

Deleting through the website is safe because GitHub checks whether the branch has been merged into your default branch. If it hasn't, GitHub shows a warning so you can decide whether to proceed. This gives you a chance to catch branches with unmerged work before they disappear.

Key Takeaways

  • The GitHub website lets you delete a branch in seconds by clicking the trash icon in the Branches tab, with a confirmation step to prevent accidents.
  • You cannot delete your default branch through the web interface — you must change your default branch first in repository settings.
  • GitHub warns you if a branch has not been merged, giving you a chance to review unmerged work before deletion.
  • From the command line, use git push origin --delete branch-name to delete a remote branch and git branch -d branch-name to delete a local copy.
  • Deleting a branch does not erase your commit history — the commits remain in GitHub's records and can be recovered if needed.

Delete a branch from the command line

If you work in a terminal, you can delete both your local branch and the remote branch on GitHub with two commands. First, delete your local copy by typing git branch -d branch-name (replace branch-name with the actual name). If the branch has unmerged commits, Git will refuse and show an error — use git branch -D branch-name in capital letters to force deletion anyway.

Next, delete the remote branch on GitHub by typing git push origin --delete branch-name. This removes the branch from your GitHub repository. Both steps are necessary: deleting locally only removes your copy on your computer, while the remote delete removes it from GitHub itself.

The command line is useful when you are already working in a terminal or when you want to delete multiple branches at once. You can also use git branch -a to list all branches (local and remote) so you can see exactly which one you are about to delete.

What happens to commits after you delete a branch

Deleting a branch does not erase the commits that were on it. If the branch was merged into another branch before deletion, those commits stay in your repository history under the branch they were merged into. If the branch was not merged, the commits still exist in GitHub's database — they are just no longer pointed to by a branch name.

GitHub keeps a record of deleted branches for 90 days. During that window, if you realize you deleted the wrong branch, you can recover it by clicking Restore on the branch's page (GitHub shows a "Restore" button briefly after deletion). After 90 days, the commits are permanently removed if they were not merged elsewhere.

This is why GitHub warns you before deleting an unmerged branch — it is your signal to review whether those commits should be saved somewhere else first. If you have work on a branch that is not yet merged, consider creating a pull request or merging it manually before deletion.

Delete a branch after merging a pull request

When you merge a pull request on GitHub, you see a button that says Delete branch right after the merge completes. Clicking it removes the branch immediately since the work is now merged into your main branch. This is the safest time to delete because you know the commits are preserved in your default branch.

Many teams set up their repositories to delete branches automatically after every merge. This keeps the branch list clean and prevents old branches from piling up. You can turn this on in your repository settings under Code and automation > Branches — look for the option "Automatically delete head branches."

If you forget to delete the branch after merging, you can still delete it later using any of the methods above. The commits are already safe in your merged code, so there is no risk.

Change your default branch before deleting it

Your default branch is the one GitHub shows when someone opens your repository, and it is the branch that pull requests merge into by default. GitHub prevents deletion of the default branch to avoid breaking the repository. If you truly need to delete it, you must first switch the default to a different branch.

Go to your repository settings, click the Branches section on the left, and look for "Default branch." Click the dropdown next to the current default branch name and choose a different branch. Click Update to save. Now you can delete the old default branch using the normal deletion methods.

This is rarely necessary — most teams keep their default branch (usually main) as a permanent record of the project. If you are thinking about deleting your default branch, consider whether you actually need a different branch to be the default instead.

Clean up old branches in bulk

If your repository has accumulated dozens of old branches, deleting them one at a time is tedious. GitHub does not have a built-in bulk delete feature, but you can delete multiple branches quickly from the command line. Type git branch -a to list all branches, then use git push origin --delete branch1 branch2 branch3 to delete several at once (replace the branch names with the ones you want removed).

Before doing bulk deletion, make sure you have merged or backed up any work you want to keep. A safer approach is to use the GitHub web interface to delete branches one at a time — it is slower but gives you a chance to review each one and see the merge status.

Some teams use branch naming patterns to make cleanup easier — for example, prefixing feature branches with "feature/" or "bugfix/" so they are easy to spot and delete together. This is a good practice if you create many short-lived branches.

Frequently Asked Questions

Can I recover a branch after I delete it?

Yes, but only within 90 days. GitHub keeps deleted branches in its database for three months. Go to your repository's Branches page and look for a "Restore" button next to the deleted branch name. After 90 days, the branch cannot be recovered unless the commits were merged into another branch first.

What is the difference between deleting locally and deleting on GitHub?

Deleting locally with git branch -d removes the branch only from your computer. Deleting remotely with git push origin --delete removes it from GitHub. You usually want to do both so the branch is gone everywhere. If you delete only locally, the branch still exists on GitHub and others can see it.

Will deleting a branch break my code?

No. Deleting a branch only removes the branch pointer — it does not touch any code that was already merged into other branches. If the branch had unmerged commits, those commits disappear from view but remain in GitHub's history for 90 days.

Can I delete someone else's branch?

Only if you have write access to the repository. On public repositories, you need to be a collaborator or maintainer. On private repositories, it depends on the permissions the owner set for your account. If you cannot see the delete button, you do not have permission.

Should I delete branches after merging?

Yes, it is a good practice. Deleting merged branches keeps your repository clean and makes it easier to find active work. Since the commits are preserved in the merged code, there is no risk. Many teams turn on automatic deletion in their repository settings so branches disappear right after merge.