Delete a branch when you no longer need it

A branch in GitHub is a separate copy of your project where you can make changes without affecting the main version. Once you finish working on a branch — after your changes are merged into the main branch or if you decide not to use it — you can delete it to keep your repository clean. Deleting a branch removes it from GitHub but does not delete any work that was already merged into another branch.

You can delete a branch in two ways: through the GitHub website (the easiest method for most people) or through the command line on your computer. Both methods take less than a minute.

Key Takeaways

  • Deleting a branch from GitHub removes it from your repository but does not erase any work that was merged into the main branch.
  • The simplest way to delete a branch is through the GitHub website by finding the branch in the Branches section and clicking the delete icon.
  • You can also delete a branch from your computer's command line using the git push origin --delete command if you prefer working in the terminal.
  • You cannot delete a branch while you are currently working on it — you must switch to a different branch first.
  • Deleted branches can be restored for up to three months if you change your mind, as long as you have the branch name.

Delete a branch through the GitHub website

Open your repository on GitHub 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. Find the branch you want to delete in the list.

On the right side of the branch name, you will see a trash can icon. Click it. GitHub will ask you to confirm the deletion. Click the red Delete button to finish. The branch is now removed from your repository.

This method works whether you created the branch yourself or someone else did. You do not need to be on your computer or use any special tools — just your web browser.

Delete a branch from your computer using the command line

If you work in the terminal or command prompt on your computer, you can delete a branch directly from there. First, make sure you are not currently on the branch you want to delete. Type git branch and press Enter to see which branch you are on (it will have an asterisk next to it). If you are on the branch you want to delete, type git checkout main and press Enter to switch to the main branch.

Once you are on a different branch, type git push origin --delete branch-name and press Enter, replacing branch-name with the actual name of the branch. For example, if your branch is called fix-login-bug, you would type git push origin --delete fix-login-bug. GitHub will delete the branch from your repository.

If you also want to remove the branch from your local computer (the copy on your machine), type git branch -d branch-name and press Enter. This cleans up your local workspace but does not affect what is on GitHub.

Why you might want to delete a branch

Branches pile up over time, especially in projects where many people are working. A repository with dozens of old branches becomes harder to navigate — you have to scroll through a long list to find the branch you actually need. Deleting branches you no longer use keeps the list short and makes it easier for you and your team to find active work.

Deleting a branch is also a way to signal that a piece of work is finished. When a branch is gone, everyone knows that the changes have been merged and the task is complete. It prevents confusion about whether a branch is still being worked on or has been abandoned.

What happens to your work when you delete a branch

If your branch was already merged into the main branch (or another branch), deleting it does not erase your work. The changes you made are still there in the main branch. Deleting only removes the branch itself — the separate copy where you made those changes.

If you delete a branch that was not merged, the work on that branch is no longer visible in your repository. However, GitHub keeps a record of deleted branches for about three months. If you realize you need the branch back, you can restore it during that window by contacting GitHub support or by using the command line if you know the exact commit hash.

Prevent accidental deletion of important branches

GitHub allows you to protect certain branches so they cannot be deleted by mistake. The main branch is often protected this way. To protect a branch, go to your repository settings, click Branches on the left side, and then click Add rule under Branch protection rules. Type the branch name you want to protect and check the box for Require a pull request before merging or other protections you want. Protected branches cannot be deleted through the normal delete process.

This is especially useful in team projects where you want to make sure the main branch stays safe and cannot be removed by accident.

Frequently Asked Questions

Can I delete a branch if I am currently working on it?

No. You must switch to a different branch first. Use git checkout main or click a different branch on the GitHub website before you delete. GitHub will show an error if you try to delete the branch you are on.

Will deleting a branch remove my code from the main branch?

No. If your changes were already merged into the main branch, they stay there. Deleting the branch only removes the separate copy where you made those changes. Your work remains in the main branch.

Can I get a deleted branch back?

GitHub keeps deleted branches for about three months. If you need to restore one, you can contact GitHub support with the branch name. After three months, the branch cannot be recovered.

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

git branch -d removes the branch from your computer only. git push origin --delete removes it from GitHub. To fully delete a branch, you usually want to do both — remove it from GitHub first, then clean up your local copy.

Do I need permission to delete a branch?

Yes. You need write access to the repository. If you are working on someone else's project and do not have permission, you will not be able to delete branches. Ask the repository owner to give you the necessary access level.