How to delete a Git branch both locally and remotely?

0 votes
811 views
asked Nov 20, 2017 by Hitesh Garg (799 points)  

I have to delete a branch both locally and remotely in a Git repository.
I can delete the local branch and also the remote branch from the browser or UI but unable to achieve the same using the git commands.
Please suggest.

1 Answer

+1 vote
answered May 23, 2018 by Rahul Singh (682 points)  
selected May 24, 2018 by Hitesh Garg
 
Best answer

Summary of commands

Delete remote branch - $ git push -d <branch_name>
Delete local branch - $ git branch -d <branch_name>

Note

  1. that in most cases the remote name is origin.
  2. -d is the alias name of --delete (with double '-')

Delete Local Branch

To delete the local branch use one of the following:

$ git branch -d branch_name
$ git branch -D branch_name

Note:

  1. First command deletes the branch if it has already been fully merged in its upstream branch.
  2. Second option, i.e. command with -D, is an alias for --delete --force, which
    deletes the branch "irrespective of its merged status."

Delete Remote Branch

$ git push <remote_name> -d <branch_name>
$ git push <remote_name> :<branch_name>

Example -
git push origin -d bugfix/make-ui-changes
git push origin :bugfix/make-ui-changes

...