In this tutorial we are going to explain how to delete a branch locally and remote using git.
What do I need to do differently to successfully delete the
remotes/origin/bugfix branch both locally and on GitHub?
Delete current branch
In this tutorial we assume that you are using origin as remote name, but it could have another name.
$ git branch # will output current branch $ git push origin --delete ## deletes remote branch on origin $ git branch -d ## Delete Local Branch
Delete branch using the name
To delete the local branch using the name execute:
$ git branch --delete branch_name
You can use
-d as a shortcut for
--delete. This command will delete local branch if it has already been fully merged
in its upstream branch.
If you want to delete the even if it was not merged:
$ git branch --delete --force branch_name
As a shortcut you can use
-D, which is an alias for
Other ways to delete git Remote Branch
$ git push origin : # deletes current branch $ git push origin :branch_name # deletes the branch_name
Deleting tags with git
If you need to change the tagged commit or the name of the tag, you will need to delete it with the commands:
git tag -d v1.0 # delete local tag git push origin :refs/tags/v1.0 # deletes tah on origin
Delete remote-tracking branches
If you deleted branches without the command line, then your local repo could contain obsolete remote-tracking branches.
This can happen if you deleted a remote branch directly through web interface. To delete remote-tracking branches execute:
git fetch origin --prune
Appendix: What is a remote tracking branch?
Remote-tracking branches are used to know what remote branch to get chnages from (git pull/fetch) and if you have remote-tracking when you execute git status it will provide information about commit behind or ahead. It's used to compare your local branch with a remote branch.
Appendix: More information about branchs
If you need to list remote branches you can execute:
git branch --remotes git branch -r # same as above.
If you need to view local and remote branches:
git branch --all git branch -a # same as above