I want to delete a branch both locally and remotely.
$ git branch -d
To delete locally - (normal)
git branch -d my_branch
If your branch is in a rebasing/merging progress and that was not done properly, it means you will get an error, Rebase/Merge in progress
, so in that case, you won't be able to delete your branch.
So either you need to solve the rebasing/merging. Otherwise, you can do force delete by using,
git branch -D my_branch
To delete in remote:
git push --delete origin my_branch
You can do the same using:
git push origin :my_branch # Easy to remember both will do the same.
Graphical representation:
Matthew's answer is great for removing remote branches and I also appreciate the explanation, but to make a simple distinction between the two commands:
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}
Reference: Git: Delete a branch (local or remote)
If you want to delete a branch, first checkout to the branch other than the branch to be deleted.
git checkout other_than_branch_to_be_deleted
Deleting the local branch:
git branch -D branch_to_be_deleted
Deleting the remote branch:
git push origin --delete branch_to_be_deleted
Delete locally:
To delete a local branch, you can use:
git branch -d <branch_name>
To delete a branch forcibly, use -D
instead of -d
.
git branch -D <branch_name>
Delete remotely:
There are two options:
git push origin :branchname
git push origin --delete branchname
I would suggest you use the second way as it is more intuitive.
Simply say:
git branch -d <branch-name>
git push origin :<branch-name>
git push origin --delete <branch Name>
is easier to remember than
git push origin :branchName