How to Delete a Branch in Git: Local, Remote, and GitHub
Deleting old or merged branches keeps your repository clean and your team's workflow uncluttered. Git distinguishes between local and remote branches — they are completely separate objects, and deleting one does not automatically delete the other.
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
Deleting a local branch
Use the git branch command with the -d flag:
$ git branch -d <local-branch>
Git will refuse this if the branch contains commits that haven't been merged or pushed yet — a safety net against accidental data loss. To override that and force-delete the branch anyway, use the -D flag:
$ git branch -D <local-branch>
Use -D with care: it permanently discards any unmerged commits on that branch.
Note that you cannot delete the branch you currently have checked out. Switch to another branch first (e.g., git checkout main) before running the delete command.
Deleting a remote branch
To delete a remote branch, you use git push with the --delete flag instead of git branch. Before you delete anything, it's a good idea to confirm the exact name of the remote branch with git branch -r, which lists all remote branches your local repository knows about — trying to delete a branch that doesn't exist on the remote only produces an unhelpful error message:
$ git branch -r
origin/feature/login
origin/main
Once you've verified the branch name, run the delete command:
$ git push origin --delete feature/login
Git also accepts an older shorthand for the same operation — a colon in front of the branch name:
$ git push origin :feature/login
The empty source before the colon tells Git to push "nothing" to that remote branch, which effectively deletes it. Both commands are equivalent; --delete is simply more explicit and easier to remember.
Either way, this only removes the remote branch. Your local branch with the same name is unaffected and must be deleted separately with git branch -d.
After a collaborator has deleted a remote branch, everyone else should run git fetch --prune (or git remote prune origin) to remove their stale local references to that branch.
Deleting a branch on GitHub
GitHub's web interface lets you delete remote branches without touching the command line. Navigate to your repository and click the Branches link, then click the trash icon next to the branch you want to remove:
You can also delete a branch directly from a pull request page once it has been merged — GitHub shows a "Delete branch" button at the bottom of the PR.
Note that GitHub only lets you delete remote branches this way. To remove the corresponding local branch on your machine, you still need to run git branch -d <branch-name> in your terminal.
Tip
Deleting (and Undoing) Branches in Tower
In case you are using the Tower Git client, you can delete any branch by right-clicking it in the sidebar and choosing the "Delete…" option. And if you change your mind, just press CMD+Z (or CTRL+Z on Windows) to instantly undo the deletion and restore the branch:
Can I undo deleting a branch?
In most cases, yes — as long as you act before Git's garbage collector runs (typically after 30 days).
On the command line, use git reflog to find the SHA of the last commit on the deleted branch, then recreate it:
$ git checkout -b <branch-name> <commit-hash>
Learn more about this technique in our free First Aid Kit for Git video series.
Tip
Undoing a Branch Deletion in Tower
If you're using the Tower Git client, simply press CMD+Z (or CTRL+Z on Windows) to undo the deletion immediately — no reflog spelunking needed.
Learn More
- Check out the chapter Deleting Branches in our free online book
- Check out the chapter Branching can Change Your Life in our free online book
Get our popular Git Cheat Sheet for free!
You'll find the most important commands on the front and helpful best practice tips on the back. Over 100,000 developers have downloaded it to make Git a little bit easier.
About Us
As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.
Just like with Tower, our mission with this platform is to help people become better professionals.
That's why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.