How to delete a branch in Git

I use a popular Git workflow called feature branching. It starts with creating a new local branch (git checkout -b my-new-branch) and then pushing it up to the central repository (git push -u origin my-new-branch). But once you’ve finished working on your branch, and you’ve successfully merged it, how do you delete it?

There are four steps:

  1. Delete the branch on GitHub
  2. Delete the local branch
  3. Delete the reference to the remote branch
  4. Verify that the branch is gone

This post focuses on GitHub, but the concepts are the same for other providers like Bitbucket and GitLab.

1. Delete the branch on GitHub

Once you’ve successfully merged your pull request, GitHub will give you the option to delete the branch used for the pull request. If you don’t do it then, you can still delete the branch at any time after you’ve merged it.

A screenshot showing how to delete a merged branch on GitHub.com.

2. Delete the local branch

  1. Run git checkout master to switch back to the master branch.
  2. Then run git pull so your master branch is up to date with the merge.
  3. Now run git branch -d my-new-branch to delete the local branch.

3. Delete the reference to the remote branch

Just run git fetch --prune and you’re done! The --prune flag removes any remote-tracking references that no longer exist on the remote.

4. Verify that the branch is gone

To verify that the branch is truly gone, run git branch -a. This will show you a list of all branches, local and remote.


If you have questions, feedback, or any other suggestions, please do email me. I'd love to hear from you!