How to Git Delete Branch Remote (and locally)

If you use Git as your version control server, it is very probable that you need to delete a remote branch. In this post, I will show you how to delete a branch locally and remotely.

Display local and remote git branches

To delete a local branch, first, we will need to print and display all our branches. We can get that using the command git branch -a, the argument -a. This command will display all available branches in the server:

git branch -a
# This will print our branches as:
# *master
#   testbranch
#   remote/rbranch/master
#   remote/rbranch/testbranch

In the case I showed you above, we have a local branch testbranch and two other remote branches.

Delete a local branch in git

Now, to delete the local branch we can simply use the same command again. But in this case, passing the argument -d followed by the name of the branch:

git branch -d testbranch
# Deleted branch testbranch

If everything went OK, the command will return the message “Deleted branch testbranch” (of course, with your own name of the branch). If an error is raised, you can use the command -D which is the same as -d –force. It will force the deletion of the branch regardless of the status of the merge.

git branch -D testbranch
# or, similarly:
git branch -d --force testbranch

Delete remote branch in git

Next, to delete a remote branch we can’t use the git branch command. Instead, we have to use a normal git push with a –delete flag containing the name of the branch. Let me show you:

First we print the branches:

git branch -a
# *master
#   testbranch
#   remote/rbranch/master
#   remote/rbranch/testbranch

Now we have to use the command git push followed by the name of the remote and the name of the branch. If we want to delete testbranch from the remote rbranch:

git push rbranch --delete testbranch
# It will print the output:
#  - [deleted]         testbranch
Git delete branch remote and local

I hope this helps you deleting branches on a git server. If you need any help, why don’t you write it here in the comments section? I will try to answer you!

Check more posts written by us

Leave a Comment