15075

I want to delete a branch both locally and remotely.

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
From github.com:gituser/gitproject
* [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?

  • 883
    Moderator note: If you intend to answer this question, do note that there are already 40 answers posted. Will your new answer add any substantial value? – Robert Harvey Jun 11 '14 at 16:10
  • 48
    Note: for Git 2.5+ (Q2 2015), the exact message will be "deleted remote-tracking branch": see github.com/git/git/commit/… – VonC May 25 '15 at 14:57

39 Answers 39

30

I also had similar issues, and this seems to work: This deletes local branch. git branch -d the_local_branch

This removes remote branch git push origin :the_remote_branch

Source: Makandra Cards

44
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
1736

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:

git push origin --delete <branch>  # Git version 1.7.0 or newer
git push origin :<branch>          # Git versions older than 1.7.0

Deleting a local branch:

git branch --delete <branch>
git branch -d <branch> # Shorter version
git branch -D <branch> # Force delete un-merged branches

Deleting a local remote-tracking branch:

git branch --delete --remotes <remote>/<branch>
git branch -dr <remote>/<branch> # Shorter

git fetch <remote> --prune # Delete multiple obsolete tracking branches
git fetch <remote> -p      # Shorter

The Long Answer: there are 3 different branches to delete!

When you're dealing with deleting branches both locally and remotely, keep in mind that there are 3 different branches involved:

  1. The local branch X.
  2. The remote origin branch X.
  3. The local remote-tracking branch origin/X that tracks the remote branch X.

Visualization of 3 branches

The original poster used

git branch -rd origin/bugfix

which only deleted his local remote-tracking branch origin/bugfix, and not the actual remote branch bugfix on origin.

Diagram 2

To delete that actual remote branch, you need

git push origin --delete bugfix

Diagram 3

Additional Details

The following sections describe additional details to consider when deleting your remote and remote-tracking branches.

Pushing to delete remote branches also deletes remote-tracking branches

Note that deleting the remote branch X from the command line using a git push will also delete the local remote-tracking branch origin/X, so it is not necessary to prune the obsolete remote-tracking branch with git fetch --prune or git fetch -p, though it wouldn't hurt if you did it anyway.

You can verify that the remote-tracking branch origin/X was also deleted by running the following:

# View just remote-tracking branches
git branch --remotes
git branch -r

# View both strictly local as well as remote-tracking branches
git branch --all
git branch -a

Pruning the obsolete local remote-tracking branch origin/X

If you didn't delete your remote branch X from the command line (like above), then your local repo will still contain (a now obsolete) remote-tracking branch origin/X. This can happen if you deleted a remote branch directly through GitHub's web interface, for example.

A typical way to remove these obsolete remote-tracking branches (since Git version 1.6.6) is to simply run git fetch with the --prune or shorter -p. Note that this removes all obsolete local remote-tracking branches for any remote branches that no longer exist on the remote:

git fetch origin --prune
git fetch origin -p # Shorter

Here is the relevant quote from the 1.6.6 release notes (emphasis mine):

"git fetch" learned --all and --multipleoptions, to run fetch from many repositories, and --prune option to remove remote tracking branches that went stale. These make "git remote update" and "git remote prune" less necessary (there is no plan to remove "remote update" nor "remote prune", though).

Alternative to above automatic pruning for obsolete remote-tracking branches

Alternatively, instead of pruning your obsolete local remote-tracking branches through git fetch -p, you can avoid making the extra network operation by just manually removing the branch(es) with the --remote or -r flags:

git branch --delete --remotes origin/X
git branch -dr origin/X # Shorter

See Also

  • From your illustration, I can see there are local clone repo and remote origin repo. So there are at least two physical branches. Where is the third branch to delete? Is the third branch only a pointer pointing to a commit in the local clone repo? – huggie Feb 18 '16 at 2:00
  • 5
    @huggie that's pretty much correct. Branches in Git are just bookmarks attached to commits. So in my graphs above, there are X and origin/X bookmarks in the local clone (2 branches), and then there is X on the remote (making 3 branches). – user456814 Feb 23 '16 at 7:33
  • 6
    +1 for the remote tracking branch. This branch is what causes issues when you clone someone else's branch. It keeps on tracking your commits and asking you if you want to push to that person's branch. – Kermit_ice_tea Jun 21 '17 at 19:48
86

Delete remote branch

git push origin :<branchname>

Delete local branch

git branch -D <branchname>

Delete local branch steps:

  1. checkout to another branch
  2. delete local branch
  • 1
    Does the remote branch deletion requires "git push" afterwards ? – Samitha Chathuranga Feb 17 '16 at 8:32
  • @SamithaChathuranga no, git push origin :<branchname> already pushes an 'empty` branch to the remote (hence deletes the remote branch) – Michał Szajbe Jun 9 '17 at 22:20
90

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
356

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
220

This is simple: Just run the following command:

To delete a Git branch both locally and remotely, first delete the local branch using command:

git branch -d example

(here example is the branch name)

And after that delete remote branch using command:

git push origin :example
256
git branch -D <name-of-branch>
git branch -D -r origin/<name-of-branch>
git push origin :<name-of-branch>
  • 6
    Note that -D forces the deletion. It's always better to use -d, which will remind if you need to do something dangerous. – Jonathon Reinhart Jan 10 '15 at 1:08
  • 11
    ahahah :) it's up to you: use -d if you want to see git crying or -D if you want to cry. – Felipe Feb 13 '15 at 11:21
119

If you want to complete both these steps with a single command, you can make an alias for it by adding the below to your ~/.gitconfig:

[alias]
    rmbranch = "!f(){ git branch -d ${1} && git push origin --delete ${1}; };f"

Alternatively, you can add this to your global config from the command line using

git config --global alias.rmbranch \
'!f(){ git branch -d ${1} && git push origin --delete ${1}; };f'

NOTE: If using -d (lowercase d), the branch will only be deleted if it has been merged. To force the delete to happen, you will need to use -D (uppercase D).

  • 4
    This is what I was looking for. My own shell function alias didn't work (Unexpected EOF) and I couldn't figure out why, but this works great! The only change I made was replacing && with ; so that even if the first command fails the second will still execute (sometimes only local or only remote exists). – user1021726 Dec 16 '14 at 8:55

protected by Community Jan 20 '13 at 21:48

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.