Categories
git git-branch git-push git-remote version-control

How do I delete a Git branch locally and remotely?

19376

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

* [new branch] bugfix -> origin/bugfix
Already up-to-date.

How do I properly delete the remotes/origin/bugfix branch both locally and remotely?

10

  • 1424

    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?

    Jun 11, 2014 at 16:10


  • 75

    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, 2015 at 14:57


  • 20

    Skip the first few answers and just jump down to the best one: stackoverflow.com/a/23961231/4561887. To delete a git branch, there are actually 3 different branches to delete! This answer makes that fact clear.

    Apr 3, 2020 at 20:37

  • 4

    @GabrielStaples Without context, your comment is confusing. Are the “best” rated answers wrong somehow?

    – Nathan

    Jun 19, 2020 at 4:32

  • 10

    @Nathan, no, they’re not wrong, but they don’t teach what you don’t know you don’t know, whereas the one I link to makes this critical unknown unknown become a known unknown and then a known known. I didn’t know you had a 1) local branch, 2) locally-stored remote-tracking branch, and 3) remote branch until I read that answer. Prior to that I thought there was only a local branch and remote branch. The locally-stored remote-tracking branch was an unknown unknown. Making it go from that to a known known is what makes that answer the best.

    Nov 18, 2020 at 23:06


24335

Executive Summary

$ git push -d <remote_name> <branchname>
$ git branch -d <branchname>

Note: In most cases, <remote_name> will be origin.

Delete Local Branch

To delete the local branch use one of the following:

$ git branch -d <branch_name>
$ git branch -D <branch_name>
  • The -d option is an alias for --delete, which only deletes the branch if it has already been fully merged in its upstream branch.
  • The -D option is an alias for --delete --force, which deletes the branch “irrespective of its merged status.” [Source: man git-branch]
  • As of Git v2.3, git branch -d (delete) learned to honor the -f (force) flag.
  • You will receive an error if you try to delete the currently selected branch.

Delete Remote Branch

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

Delete Remote Branch [Original Answer from 5-Jan-2010]

From Chapter 3 of Pro Git by Scott Chacon:

Deleting Remote Branches

Suppose you’re done with a remote branch — say, you and your collaborators are finished with a feature and have merged it into your remote’s main branch (or whatever branch your stable code-line is in). You can delete a remote branch using the rather obtuse syntax git push [remotename] :[branch]. If you want to delete your server-fix branch from the server, you run the following:

$ git push origin :serverfix
To [email protected]:schacon/simplegit.git
 - [deleted]         serverfix

Boom. No more branches on your server. You may want to dog-ear this page, because you’ll need that command, and you’ll likely forget the syntax. A way to remember this command is by recalling the git push [remotename] [localbranch]:[remotebranch] syntax that we went over a bit earlier. If you leave off the [localbranch] portion, then you’re basically saying, “Take nothing on my side and make it be [remotebranch].”

I issued git push origin: bugfix and it worked beautifully. Scott Chacon was right—I will want to dog ear that page (or virtually dog ear by answering this on Stack Overflow).

Then you should execute this on other machines

# Fetch changes from all remotes and locally delete 
# remote deleted branches/tags etc
# --prune will do the job :-;
git fetch --all --prune

to propagate changes.

28

  • 489

    Don’t forget to do a git fetch --all --prune on other machines after deleting the remote branch on the server. ||| After deleting the local branch with git branch -d and deleting the remote branch with git push origin --delete other machines may still have “obsolete tracking branches” (to see them do git branch -a). To get rid of these do git fetch --all --prune.

    May 27, 2015 at 16:51

  • 25

    in addition to @TrevorBoydSmith’s git branch -a to view all branches, you can also use git branch -r to view remote branches only. see also git remote show origin – source: gitready.com/intermediate/2009/02/13/list-remote-branches.html

    – Sandra

    Sep 9, 2015 at 9:53

  • 29

    I had to run git branch -D Branch_Name to get rid of the local branch

    Mar 10, 2016 at 1:32

  • 19

    @KolobCanyon You only have to use -D if the branch has not been merged into another branch.

    – BTRUE

    Apr 5, 2016 at 16:27

  • 8

    The question was “What do I need to do differently to successfully delete the remotes/origin/bugfix branch both locally and on GitHub?” After running the commands in your updated answer, the local branch is still present. It would be nice if the accepted answer was a complete answer. Its absolutely amazing at how difficult Git makes simple tasks…

    – jww

    Jun 7, 2016 at 2:15


3634

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)

9

  • 272

    @megido well -D force deletes, -d gives you a warning if it’s not already merged in.

    Nov 6, 2014 at 19:04

  • 10

    If your local branch is not merge with master and ran 'git branch -d your_branch then you will error like error: The branch 'your_branch' is not fully merged. If you are sure you want to delete it, run 'git branch -D your_branch'.

    Oct 31, 2015 at 12:59


  • 17

    I would suggest using -d instead of -D because it is safer. If -d fails due to unmerged commits then you need to assess that and if it is definitely OK to remove then use -D.

    – rooby

    Feb 2, 2016 at 3:47

  • 9

    Others with repository clones where remote branches have been removed should run git remote prune <name> (e.g. git remote prune origin) in order to locally remove stale branches that no longer exist in the remote.

    Apr 18, 2016 at 23:07

  • 4

    I would like to add that -d gives a warning if it isn’t merged in with the current HEAD. If you need clarity I recommend this command git branch -a --merged origin/master It will list any branches, both local and remote; that have been merged into master. Additional information here

    – Eric

    Feb 10, 2017 at 13:10

2442

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 -d <branch>        # Shorter version (Git 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 remote-tracking branches
git fetch <remote> -p      # Shorter

The long answer: there are three different branches to delete!

When you’re dealing with deleting branches both locally and remotely, keep in mind that there are three 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 three 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 removes remote-tracking branches

Note that deleting the remote branch X from the command line using a git push will also remove 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. However, 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 repository 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 --multiple options, 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 --remotes or -r flags:

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

See Also

8

  • 7

    @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, 2016 at 7:33

  • 10

    +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.

    Jun 21, 2017 at 19:48

  • 2

    For the sake of future readers: What @Kermit_ice_tea is talking about above is a local branch (as described in this answer), not a remote-tracking branch. When a local branch has an “upstream branch” configured for it, it will by default pull from and push to that remote branch. A local branch that has an “upstream branch” set on it is referred to as a “tracking branch”, so it’s easy to confuse with remote-tracking branches due to the similar terminology.

    – David P

    Mar 5, 2020 at 11:21

  • 5

    I’ve read all the answers down to here and this is for sure the best answer I’ve read so far!–(and probably the best one on this page, period). This is especially true because it’s the only answer which states this REALLY IMPORTANT fact that I never knew before: “there are 3 different branches to delete!” I had no idea! This all makes so much more sense now, and it sheds so much light on all the other answers here now too. Thanks!

    Apr 3, 2020 at 20:35

  • 4

    It should be noted that running git push origin --delete <branch>, as far as I can tell, ALSO deletes the locally-stored remote-tracking branch named origin/branch. So, to delete the remote branch AND locally-stored remote-tracking branch in one command, just use git push origin --delete <branch>. Then, you just need to delete the local branch with git branch -D branch. That covers the deletion of all 3 branches with only 2 commands.

    Jan 5, 2021 at 0:40