How do I delete a Git branch locally and remotely?
Share
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Delete Local Branch
To delete the local branch use one of the following:
-d
option is an alias for--delete
, which only deletes the branch if it has already been fully merged in its upstream branch.-D
option is an alias for--delete --force
, which deletes the branch “irrespective of its merged status.” [Source:man git-branch
]git branch -d
(delete) learned to honor the-f
(force) flag.Delete Remote Branch
As of Git v1.7.0, you can delete a remote branch using
which might be easier to remember than
which was added in Git v1.5.0 “to delete a remote branch or a tag.”
Starting with Git v2.8.0, you can also use
git push
with the-d
option as an alias for--delete
. Therefore, the version of Git you have installed will dictate whether you need to use the easier or harder syntax.To remove a local branch from your machine:
git branch -d {the_local_branch}
(use-D
instead to force deleting the branch without checking merged status)To remove a remote branch from the server:
git push origin --delete {the_remote_branch}
The short answers
If you want more detailed explanations of the following commands, then see the long answers in the next section.
Deleting a remote branch
Deleting a local branch
It’s very simple:
To delete the remote branch
Or
— You can also delete tags with this syntax
To forcefully delete local branch
If you want to delete a branch, first checkout to the branch other than the branch to be deleted.
Deleting the local branch:
Deleting the remote branch:
Delete locally:
To delete a local branch, you can use:
To delete a branch forcibly, use
-D
instead of-d
.Delete remotely:
There are two options:
To delete your branch locally and remotely
git checkout master
git push origin --delete <branch-name>
git branch --delete <branch-name>