in Git by (32.2k points)
How do I delete a Git branch locally and remotely?

I want to delete a branch both locally and remotely.

Failed Attempts to Delete a Remote Branch

$ git branch -d remotes/origin/bugfix

error: branch 'remotes/origin/bugfix' not found.

$ git branch -d origin/bugfix

error: branch 'origin/bugfix' not found.

$ git branch -rd origin/bugfix

Deleted remote branch origin/bugfix (was 2a14ef7).

$ git push

Everything up-to-date

$ git pull


* [new branch] bugfix -> origin/bugfix

Already up-to-date.

What should I do differently to successfully delete the remotes/origin/bugfix branch both locally and remotely?

1 Answer

0 votes
by (32.2k points)
Executive Summary

$ git push -d <remote_name> <branch_name>

$ git branch -d <branch_name>

Note that in most cases the remote name is origin. In such a case you'll have to use the command like so.

$ git push -d origin <branch_name>

Delete Local Branch

To delete the local branch use one of the following:

$ git branch -d branch_name

$ git branch -D branch_name

Note: The -d option is an alias for --delete, which only deletes the branch if it has already

been fully merged in its upstream branch. You could also use -D, which is an alias for --delete --force, which deletes the branch "irrespective of its merged status." [Source: man git-branch]

Also note that git branch -d branch_name will fail if you are currently in the branch you want to remove. The message starts with error: Cannot delete the branch 'branch_name'. If so, first switch to some other branch, for example: git checkout master.

Delete Remote Branch [Updated on 8-Sep-2017]

As of Git v1.7.0, you can delete a remote branch using

$ git push <remote_name> --delete <branch_name>

which might be easier to remember than

$ git push <remote_name> :<branch_name>

which was added in Git v1.5.0 "to delete a remote branch or a tag."

Starting on 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.

Related questions

0 votes
0 votes
asked May 21, 2020 in Git by SakshiSharma (32.2k points)