How to delete a Git branch both locally and remotely?

0 votes
101 views
asked Nov 21, 2017 by Hitesh Garg (316 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 9 hours ago by Rahul Singh (428 points)  
selected 5 hours ago 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

Welcome to Codingeek Q&A, where you can ask questions and receive answers from other members of the community.
...