How to delete Git branch locally and remotely ?

While working with Source version control systems like Git. We may come across scenarios where you need to delete feature branch remotely or delete branch which you have created accidentally in your local system.

In this tutorial, We will understand how to do it using Git commands.

How to delete branch locally:

git branch -d <branch_name>

Here, -d is an alias name of –delete in Git. Which can be used interchangeably.

You can also use below command to force delete:

git branch -D <branch_name>

Here, -D is an alias name of –delete–force in Git.

Suppose you made your code into your feature branch and then you have merged the same into Master branch and you wish to delete your feature branch from locally and remotely as well.

In that scenario, First you need to delete your branch using above command and then you can use below commands to delete it remotely.

git push <remote_location_URL> --delete <branch_name>                               Git v1.7.0

or

git push <remote_location_URL> -d <branch_name>                                           Git v2.8.0

or

git push <remote_location_URL> :<branch_name>                                                Git v1.5.0

Please note that here -d is an alias of –delete.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.