During your development process you may well have to delete a local branch, but how do you then delete the remote git branch? In this post I’ll explain how to remove a remote git branch, and I’ll also explain how to see a list of remote branches so you know which one to remove.
Table of contents
How to list remote branches
Before you delete a remote branch you need to be very sure that you have the name spelled EXACTLY right, so it’s useful to know how to list your remote branches. This command will show you a list of them all:
git branch -r
How to delete a remote git branch
So how do you actually delete a remote branch? Use this command to remove the remote branch with the specified name :
git push origin --delete branch-name
So if you were deleting a remote branch called bugfix-PRJ824
we would type:
git push origin --delete bugfix-PRJ824
What if you want to recover a deleted remote branch?
When you use the command to remove a remote git branch as shown above, all it really does is delete the pointer to the commit (which is all a branch actually is). A Git server has a garbage collector which will periodically do a clean up, but until that has run you can easily recover a the remote branch.
There is a Maintenance & Data Recovery section of the official Git documentation which describes the process to recover a deleted branch.
When can’t you delete a remote branch?
If the remote repository has the branch security set such that you do not have permission to delete the branch, then you may see a message that’s something like this:
permission denied to delete branch (branch-name)
In this case your only option is to contact the repository admin to request permission, or ask them to delete it for you.
Summary
So now you know how to list your remote branches, and how to delete a remote branch in git, and you have a resource which will help you recover one after it’s been deleted. You may also find this post useful as it explains how to delete your local git branches which no longer track a remote branch.