How do I delete a Git branch both locally and remotely?


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}



I want to delete a branch both locally and on my remote project fork on GitHub.

Failed Attempts to Delete 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 do I need to do differently to successfully delete the remotes/origin/bugfix branch both locally and on GitHub?

Another approach is

git push --prune origin

WARNING: This will delete all remote branches that do not exist locally. Or more comprehensively,

git push --mirror

will effectively make the remote repository look like the local copy of the repository (local heads, remotes and tags are mirrored on remote).

git push origin :bugfix  # Deletes remote branch
git branch -d bugfix     # Must delete local branch manually

If you are sure you want to delete it, run

git branch -D bugfix

Now to clean up deleted remote branches run

git remote prune origin

One liner command delete both local, and remote:

D=branch-name; git branch -D $D; git push origin :$D

or add the alias below to your ~/.gitconfig; usage: git kill branch-name

    kill = "!f(){ git branch -D \"$1\";  git push origin --delete \"$1\"; };f"

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 2nd way as it is more intuitive.

Now you can do it with the GitHub Desktop app.

After launching the app

  1. Click on the project containing the branch
  2. Switch to the branch you would like to delete
  3. From the "Branch" menu, select, "Unpublish...", to have the branch deleted from the GitHub servers.
  4. From the "Branch" menu, select, 'Delete "branch_name"...', to have the branch deleted off of your local machine (aka the machine you are currently working on)

Before executing

git branch --delete <branch>

make sure you determine first what the EXACT name of the remote branch is by executing:

git ls-remote

This will tell you what to enter EXACTLY for <branch> value. (branch is case sensitive!)

git push origin --delete branchName

is easier to remember than

git push origin :branchName

You can also do this using git remote prune origin:

$ git remote prune origin
Pruning origin
 * [pruned] origin/some-branchs

It prunes and deletes remote-tracking branches from a git branch -r listing.

Deleting Branches

Let's assume our work on branch "contact-form" is done and we've already integrated it into "master". Since we don't need it anymore, we can delete it (locally):

$ git branch -d contact-form

And for deleting the remote branch:

git push origin --delete contact-form

git branch -D <name-of-branch>
git branch -D -r origin/<name-of-branch>
git push origin :<name-of-branch>

Steps for deleting a branch:

for deleting the remote branch:

git push origin --delete <your_branch> 

for deleting the local branch:

git branch -D <branch_name> 

Explain: OK, just explain what's going on here!

Simply do git push origin --delete to delete your remote branch, add the name of the branch at the end and this will delete and push it to remote at the same time...

Also, git branch -D, which simply delete the local branch Only!...

-D stands for --delete --force which will delete the branch even it's not merged(force delete), but you can also use -d which stands for --delete which throw an error respective of the branch merge status...

I also create the image below to show the steps:

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

Since January 2013, GitHub included a Delete branch button next to each branch in your "Branches" page.

Relevant blog post: Create and delete branches

Many of the other answers will lead to errors/warnings. This approach is relatively fool proof although you may still need git branch -D branch_to_delete if it's not fully merged into some_other_branch, for example.

git checkout some_other_branch
git push origin :branch_to_delete
git branch -d branch_to_delete

Remote pruning isn't needed if you deleted the remote branch. It's only used to get the most up to date remotes available on a repo you're tracking. I've observed git fetch will add remotes, not remove them. Here's an example of when git remote prune origin will actually do something:

User A does the steps above. User B would run the following commands to see the most up to date remote branches

git fetch
git remote prune origin
git branch -r