How to delete git branch locally and remotely

How to delete git branch locally and remotely

In this article lets understand how to delete git branch locally and remotely.

So, there can be two types of situations where you want to delete specific branch from git. Such as,

  1. You want to delete local branch only
  2. You want to delete remote branch from repository

Let’s understand both one by one.

1 ) Delete the branch locally:

Check below command to remove local branch.

git branch -d mybranch
  • mybranch – will be the name of your branch you want to delete.

There is one more possibility where you will have some unmerged changes into the branch and still want to force delete it. In that case use below command to force delete.

git branch -D mybranch
  • The -D here defines force delete.
  • mybranch – will be the name of your branch you want to delete.

2 ) Delete the branch on the remote repository:

Use below command to delete the remote branch from your repository.

git push origin --delete mybranch

On the other hand there is another shorthand command you can use as below:

git push origin :mybranch

mybranch – will be the name of your branch you want to delete.

In most of cases it will be “origin” but if you have multiple remote repositories, replace origin with the appropriate remote name.

Deleting a branch is a permanent thing, and keep in mind it will be deleted from local and remote, so before applying solutions please keep a hand in backup and use the commands wisely.

Also, deleting a branch will not delete the commits, it will still be present in the hierarchy.

That’s all on how to delete git branch locally and remotely. Hope this find you helpful.

Read more articles on git here.