How do I delete a Git branch locally and remotely?
Explanation
Here we will see how can one delete a Git branch locally and remotely
Executive Summary
$ git push -d <remote_name> <branchname>
$ git branch -d <branchname>
Here we have to note that origin
will be <remote_name>
in many cases.
Delete Local Branch
So if one wants to delete any local branch at that time we can use:
$ git branch -d <branch_name>
$ git branch -D <branch_name>
-
- Here we use option
-d
. This option is an alias for--delete
. This will delete the branch which is merged in the upstream branch. - The
-D
the option is an alias for--delete --force
, which deletes the branch “irrespective of its merged status.” [Source:man git-branch
] - Also there an error will occur. If one tries to delete the branch which is currently in work and is selected.
- Here we use option
Delete Remote Branch
To delete a remote branch, there is a Git v1.7.0 available. So that you can delete a remote branch using the following:
$ git push <remote_name> --delete <branch_name>
And this is easy to remember than the following:
$ git push <remote_name> :<branch_name>
which was added in Git v1.5.0 “to delete a remote branch or a tag.”
You can also use Git v2.8.0. Using this you can also use git push
the option -d
. This is used as an alias for --delete
. Also, the Git versions are also capable to dictate by the versions which syntax you need to use. The easier or the harder one
Also, read how do I undo the most recent local commits in Git?