How can I delete branches in Git?
You might have asked yourself, "How can I properly delete this old bugfix branch both locally and remotely?"
In this article, we will show you how to delete a remote branch in Git (and GitHub automatically).
But before we look at deleting remote branches, let's discuss the syntax for deleting a local branch in Git.
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
Deleting local branches in Git
$ git branch -d feature/login
Using the "-d" flag with the "git branch" command allows you to specify which branch you want to delete.
It's worth noting that you may also require the "-f" flag when attempting to delete a branch that still has unmerged changes. Use this option with caution, as it can result in data loss quite easily.
Deleting remote branches in Git
To delete a remote branch, we do not use the "git branch" command - but instead "git push" with the "--delete" flag:
$ git push origin --delete feature/login
Tip
Deleting Branches in Tower
In case you are using the Tower Git client, you can simply right-click any branch item in the sidebar and choose the "Delete…" option to get rid of it. But even better: if you made a mistake, you can simply hit CMD+Z (or CTRL+Z on Windows) to undo the deletion and restore the branch!
That's not the only Git operation you can undo in Tower. In fact, nearly everything can be reverted by using this convenient keyboard shortcut.
Deleting both a local and a remote branch
Just a side note: please keep in mind that local and remote branches actually have nothing to do with each other. They are completely separate objects in Git.
Even if you've established a tracking connection (which you should for most scenarios), this still does not mean that deleting one would delete the other, too!
If you want any branch item to be deleted, you need to delete it explicitly. At the end, if you check on GitHub, you will see that it has also been deleted there.
Learn More
- Check out the chapter Deleting Branches in our free online book
- More frequently asked questions about Git & version control