Delete a branch you no longer need in three steps

A branch in GitHub is a separate copy of your project where you can make changes without affecting the main version. Over time, branches pile up — some finished, some abandoned, some merged into your main code. Deleting branches you no longer use keeps your repository clean and makes it easier for you and your team to see which work is actually active.

You can delete a branch in two places: on the GitHub website itself (the easiest method for most people) or on your computer using the command line. Both take less than a minute. The only rule: you cannot delete a branch that is currently checked out — meaning you cannot delete the branch you are actively working on.

Key Takeaways

  • Delete branches through the GitHub website by opening your repository, clicking the Branches tab, and selecting the delete icon next to the branch name.
  • You cannot delete a branch while you are actively working on it — switch to a different branch first.
  • Deleting a branch does not erase the work inside it if that work was already merged into another branch.
  • If you delete a branch by accident, GitHub keeps a record for about three months and you can restore it from the branch settings page.
  • Use the command line method (git branch -d branchname) if you prefer working in your terminal or need to delete multiple branches at once.

Delete a branch on the GitHub website

Open your repository on GitHub.com and click the Branches tab near the top of the page, just below the repository name. You will see a list of all branches in your project, with the most recently updated ones at the top.

Find the branch you want to delete. On the right side of that branch's row, you will see a trash can icon. Click it. GitHub will ask you to confirm — click the red Delete button. The branch is now gone from your repository.

If the trash can icon is grayed out or missing, it means that branch is currently checked out (someone is actively working on it) or it is a protected branch (your repository settings prevent deletion). Switch to a different branch first, or ask the person using that branch to switch, then try again.

Delete a branch from your computer using the command line

If you work in the terminal or command prompt, you can delete a branch locally (on your computer) and then push that deletion to GitHub. First, make sure you are not on the branch you want to delete. Type git branch to see which branch you are currently on — it will have an asterisk next to it.

Switch to a different branch by typing git checkout main (or whatever your default branch is called). Then delete the local branch by typing git branch -d branchname, replacing "branchname" with the actual name. If the branch has unpushed changes, Git will refuse to delete it and tell you to use git branch -D branchname instead (capital D) if you are certain you want to discard those changes.

After deleting locally, push the deletion to GitHub by typing git push origin --delete branchname. This removes the branch from the GitHub website as well. You can delete multiple branches at once by running the push command once for each one, or by using git push origin --delete branch1 branch2 branch3 to delete three branches in a single command.

What happens to the work inside a deleted branch

If you merged the branch into another branch (usually main) before deleting it, all the work is safe — it lives on in the branch you merged it into. Deleting the branch only removes the branch itself, not the commits it contained.

If you delete a branch that was never merged, the work inside it is still recoverable for about three months. GitHub keeps deleted branches in a recovery state. Go to your repository's Branches page, click the Deleted branches tab at the top, find the branch you want back, and click Restore. After three months, GitHub permanently removes the branch and you cannot restore it.

Protect branches so they cannot be deleted by accident

If you work on a team, you may want to prevent people from deleting important branches like main or production. Go to your repository's Settings, click Branches on the left sidebar, and click Add rule under "Branch protection rules." Type the branch name (or a pattern like main or release/*) and check the box that says Require a pull request before merging. You can also check Dismiss stale pull request approvals when new commits are pushed and other options depending on your team's workflow.

Once a branch is protected, it cannot be deleted through the GitHub website. The command line will still allow deletion, but only for users with admin access to the repository. This prevents accidental deletions while still letting your team lead manage branches when necessary.

Delete branches in bulk

If you have many old branches to clean up at once, the GitHub website method becomes tedious. Use the command line instead. Type git branch -a to list all branches (local and remote). Then use git branch -d for each one, or combine them: git branch -d branch1 branch2 branch3.

To delete all branches except main in one command, use git branch | grep -v "main" | xargs git branch -d on Mac or Linux. On Windows, this command works differently — ask your team lead or check your Git documentation for the Windows equivalent. Always double-check which branches you are about to delete before running a bulk command, because you cannot undo it as easily as a single deletion.

Frequently Asked Questions

Can I undo a branch deletion?

Yes, but only within three months. Go to your repository's Branches page, click the Deleted branches tab, find the branch, and click Restore. After three months, GitHub permanently removes the branch and you cannot get it back. If the branch was merged before deletion, the work is still in the branch it was merged into.

What if I delete a branch that has unpushed commits?

If you use the command line and try to delete a branch with unpushed work, Git will refuse and tell you to use the capital-D flag instead. If you use capital D, those commits are lost unless you have them backed up elsewhere. The GitHub website will not let you delete a branch with unpushed changes — it will show an error instead.

Do I need to delete branches, or can I just leave them?

You do not have to delete branches, but old branches clutter your repository and make it harder to find active work. Most teams delete branches after merging them. It is good practice, but not required.

Why can't I delete the main branch?

Main is usually your default branch and is often protected by your repository settings. You can unprotect it in Settings if you really need to, but most teams keep main protected to prevent accidental deletion. If you need a different default branch, change it in Settings instead of deleting main.

Can I delete a branch if someone else is working on it?

The GitHub website will not let you delete a branch that is currently checked out by someone else. They need to switch to a different branch first. If you use the command line, you can force-delete it, but this will cause problems for anyone working on that branch — avoid doing this without talking to your team first.