In this short tutorial we are going to explore how to delete a local or remote branch from git, sometimes is necessary to remove a branch and following the examples below you will be able to remove or delete those branches (remote or local).
I would recommend to set a github test repository to practice the examples below to delete the local branch.
If you want more detailed explanations of the following commands, then see the long answers in the next section.
Deleting a remote branch
git push origin --delete <branch> # Git version 1.7.0 or newer
git push origin -d <branch> # Shorter version (Git 1.7.0 or newer)
git push origin :<branch> # Git versions older than 1.7.0
Deleting a local branch
git branch --delete <branch>
git branch -d <branch> # Shorter version
git branch -D <branch> # Force-delete un-merged branches
Deleting a local remote-tracking branch
git branch --delete --remotes <remote>/<branch>
git branch -dr <remote>/<branch> # Shorter
git fetch <remote> --prune # Delete multiple obsolete remote-tracking branches
git fetch <remote> -p # Shorter
The long answer: there are three different branches to delete!
When you’re dealing with deleting branches both locally and remotely, keep in mind that there are three different branches involved:
The local branch X.
The remote origin branch X.
The local remote-tracking branch origin/X that tracks the remote branch X.
The original poster used:
git branch -rd origin/bugfix
Which only deleted his local remote-tracking branch origin/bugfix, and not the actual remote branch bugfix on origin.
To delete that actual remote branch, you need
git push origin --delete bugfix
Additional details
The following sections describe additional details to consider when deleting your remote and remote-tracking branches.
Pushing to delete remote branches also removes remote-tracking branches
Note that deleting the remote branch X from the command line using a git push will also remove the local remote-tracking branch origin/X, so it is not necessary to prune the obsolete remote-tracking branch with git fetch --prune or git fetch -p. However, it wouldn't hurt if you did it anyway.
You can verify that the remote-tracking branch origin/X was also deleted by running the following:
# View just remote-tracking branches
git branch --remotes
git branch -r
# View both strictly local as well as remote-tracking branches
git branch --all
git branch -a
Pruning the obsolete local remote-tracking branch origin/X
If you didn’t delete your remote branch X from the command line (like above), then your local repository will still contain (a now obsolete) remote-tracking branch origin/X. This can happen if you deleted a remote branch directly through GitHub's web interface, for example.
A typical way to remove these obsolete remote-tracking branches (since Git version 1.6.6) is to simply run git fetch with the --prune or shorter -p. Note that this removes all obsolete local remote-tracking branches for any remote branches that no longer exist on the remote:
git fetch origin --prune
git fetch origin -p # Shorter
Take a look at these additional resources:
Git Going Fast: One Hour Git Crash Course
Introduction to Git and GitHub
Reference links:
https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-locally-and-remotely
Other Dev posts:
Discussion