I use a popular Git workflow called feature branching. It starts with creating a new local branch and then pushing it up to the central repository. 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
Switch my-new-branch
with the name of your branch:
- Run
git checkout master
to switch back to themaster
branch. - Then run
git pull
so yourmaster
branch is up to date with the merge. - 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.