Delete a branch you no longer need

You delete a Git branch when you no longer need it — usually after you've merged your work into the main branch and closed the pull request. Git keeps branches around until you remove them, so your repository can get cluttered with old feature branches, experiment branches, and branches from work that's finished.

Deleting a branch is safe as long as the work is already merged somewhere else. If you delete a branch by accident and haven't merged it, you can usually recover it within 30 days using the reflog. The process is different depending on whether you're deleting a branch on your own computer or on a remote server like GitHub.

Key Takeaways

  • Delete a local branch on your computer with git branch -d branch-name, replacing branch-name with the actual name.
  • Delete a branch on GitHub or another remote server with git push origin --delete branch-name.
  • Use -D instead of -d to force deletion of a branch that hasn't been merged yet, though this is usually a sign you should double-check first.
  • Deleting a local branch does not delete the same branch on GitHub — you must delete both separately.
  • You cannot delete the branch you are currently on, so switch to a different branch first.

Delete a branch on your own computer

Open your terminal or command prompt and navigate to your project folder. First, make sure you're not currently on the branch you want to delete — if you are, switch to a different branch like main or master.

Type this command and press Enter:

git branch -d branch-name

Replace branch-name with the actual name of the branch you want to remove. For example, if your branch is called fix-login-bug, you would type git branch -d fix-login-bug.

If the branch has been merged into your main branch, Git will delete it immediately and show you a message confirming the deletion. If the branch has not been merged, Git will refuse to delete it and show you a warning instead. This is a safety feature to prevent you from accidentally losing work.

Delete a branch on GitHub or another remote server

After you delete a branch on your computer, it still exists on GitHub (or wherever your remote repository is hosted). You need to delete it there separately using a different command.

In your terminal, type:

git push origin --delete branch-name

Replace origin with the name of your remote if it's different — most people use origin by default. Replace branch-name with the name of the branch you're removing.

GitHub will delete the branch and show you a confirmation message. The branch is now gone from both your computer and the remote server. If you had a pull request open for this branch, GitHub will automatically close it.

Force delete a branch that hasn't been merged

If Git refuses to delete a branch because it hasn't been merged, you can force the deletion by using a capital D instead of lowercase d:

git branch -D branch-name

This tells Git to delete the branch no matter what. Before you do this, make sure the work on that branch is actually saved somewhere else — either pushed to GitHub, merged into another branch, or backed up in some way. If you force-delete a branch and the work isn't saved anywhere, you'll need to use the reflog to recover it, which is more complicated.

If you're unsure whether a branch has been merged, you can check by typing git branch -v to see all your local branches and their status, or git branch --merged to see only branches that have been merged into your current branch.

Delete multiple branches at once

If you have many old branches to clean up, you can delete several at once instead of running the command for each one individually. List the branches you want to delete separated by spaces:

git branch -d branch-one branch-two branch-three

Git will delete all three branches in one command, as long as they've all been merged. If any of them haven't been merged, Git will stop and ask you to confirm before deleting any of them.

You can also delete all branches except the one you're currently on by using a more advanced command, but that's usually overkill for most projects. Deleting branches one at a time or in small groups is safer and gives you a chance to double-check each one.

Recover a branch you deleted by accident

If you deleted a branch and realized you needed it, you can recover it using the reflog, which is Git's record of every action you've performed. Type this command:

git reflog

Look through the list for the commit where you deleted the branch — it will show something like "HEAD@{0}: branch -d branch-name". Note the reference number on the left, like HEAD@{2}. Then create a new branch from that point:

git branch branch-name HEAD@{2}

This recreates the branch at the exact state it was in before you deleted it. The reflog keeps records for about 30 days by default, so you have a window of time to recover deleted branches. After that, Git's garbage collection removes the old data permanently.

Frequently Asked Questions

What happens if I delete a branch that hasn't been merged?

Git will refuse to delete it with the regular -d command and show you a warning. You can force deletion with -D, but the work on that branch will be lost unless it's saved somewhere else. Check if the branch is pushed to GitHub or merged into another branch before forcing deletion.

Do I have to delete a branch on GitHub if I delete it locally?

No, they are separate. Deleting a branch on your computer does not touch GitHub. You must run git push origin --delete branch-name to remove it from the remote server. Many people delete the local branch first, then delete the remote branch afterward.

Can I delete the branch I'm currently on?

No, Git will not let you delete the branch you're currently working on. Switch to a different branch first by typing git checkout main or git switch main, then delete the branch you no longer need.

How do I see which branches are safe to delete?

Type git branch --merged to see all branches that have been merged into your current branch. These are usually safe to delete. Type git branch --no-merged to see branches that have not been merged yet — be careful deleting these unless you're sure the work is saved elsewhere.