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:
- Delete the branch on GitHub
- Delete the local branch
- Delete the reference to the remote branch
- 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.
2. Delete the local branch
git checkout masterto switch back to the
- Then run
git pullso your
masterbranch is up to date with the merge.
- Now run
git branch -d my-new-branchto delete the local branch.
3. Delete the reference to the remote branch
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!