How do I delete a Git branch locally and remotely?

9,892,704

Solution 1

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.

Solution 2

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)

Solution 3

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

Solution 4

Steps for deleting a branch:

For deleting the remote branch:

git push origin --delete <your_branch>

For deleting the local branch, you have three ways:

1: git branch -D <branch_name>

2: git branch --delete --force <branch_name>  # Same as -D

3: git branch --delete  <branch_name>         # Error on unmerge

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

Simply do git push origin --delete to delete your remote branch only, 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:

Delete a remote and local branch in git

Solution 5

You can also use the following to delete the remote branch

git push --delete origin serverfix

Which does the same thing as

git push origin :serverfix

but it may be easier to remember.

Share:
9,892,704
Matthew Rankin
Author by

Matthew Rankin

Updated on July 11, 2022

Comments

  • Matthew Rankin
    Matthew Rankin almost 2 years

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

    • Yinda Yin
      Yinda Yin almost 10 years
      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?
    • VonC
      VonC almost 9 years
      Note: for Git 2.5+ (Q2 2015), the exact message will be "deleted remote-tracking branch": see github.com/git/git/commit/…
    • Mig
      Mig over 4 years
      You may be interested in this script which simplifies the difference between deleting a local branch AND a remote one: tlbx.app/blog/delete-any-git-branch-the-easy-way
    • Yinda Yin
      Yinda Yin over 4 years
      @Adam: 81. 42 answers were deleted for various reasons. The public's unexplained obsession with a particular topic is one of life's greatest mysteries.
    • Gabriel Staples
      Gabriel Staples about 4 years
      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.
    • Nathan
      Nathan almost 4 years
      @GabrielStaples Without context, your comment is confusing. Are the "best" rated answers wrong somehow?
    • Gabriel Staples
      Gabriel Staples over 3 years
      @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.
    • cRAN
      cRAN about 3 years
      I wanted to add a note that one cannot delete a branch on which one is currently working upon. I have made this rookie mistake so thought if this could help. :)
    • Panzercrisis
      Panzercrisis almost 3 years
      Ordinary user note: If you intend to upvote Robert Harvey's comment, do note that there are already 1215 upvotes added. Will your new upvote add any substantial value? :) ...Now it's 1216! That is a lot.
    • RBT
      RBT almost 3 years
      A very nice blog on freecodecamp
    • Sławomir Lenart
      Sławomir Lenart over 2 years
      @RobertHarvey: Re: Moderator note people see a man with 413K rank and see all was made by this question 10*18K+ and the top answer here 10*23K+ so they are only people reacting ... me too, me too - actually this case could be a ground to add an Others reason when voting for close a question. Or kind of already well answered.
  • Annika Backstrom
    Annika Backstrom almost 11 years
    You should clarify that the above git push operation deletes the local branch and the remote branch.
  • Cam Jackson
    Cam Jackson over 10 years
    I only started using Github this year, so I was wondering why this was such a highly rated question, and why none of the top answers were suggesting to just delete it from the Github web interface! Interesting that it's only a recent addition.
  • ArtOfWarfare
    ArtOfWarfare over 10 years
    I was going to point this one out. Note that the button won't delete your local branch... see this answer for how to do that: stackoverflow.com/a/10999165/901641
  • Admin
    Admin almost 10 years
    Note that git remote prune is a somewhat obsolete way to remove obsolete remote-tracking branches, the newer way to do it is to use git fetch --prune or git fetch -p.
  • Admin
    Admin almost 10 years
    This is fine, but people really shouldn't be naming their branches and tags with the same name and same naming scheme in the first place.
  • Greg
    Greg almost 10 years
    Well, my scenario was that I was converting a branch to a tag and it made sense for the tag to have the same name as the branch. By converting I mean merging branch B to A and tagging the last commit in branch B with tag B so that after deleting branch B it can still be easily restored by simply checking out tag B.
  • TankorSmash
    TankorSmash over 9 years
    @megido well -D force deletes, -d gives you a warning if it's not already merged in.
  • hdost
    hdost over 9 years
    I ended up just add the alias "shoot" into my .gitconfig shoot = push origin --delete
  • pfrenssen
    pfrenssen over 9 years
    @RRMadhav, indeed you won't see the deleted branch after deleting it since the reference to the remote branch will be removed for you locally. Anyone else on your team that has checked out that branch will still have that reference and will still see it unless they prune the branch.
  • Dan Rosenstark
    Dan Rosenstark over 9 years
    @chhh then you need to extend this functionality to make this a variable instead of an assumption.
  • neoscribe
    neoscribe over 9 years
    If your origin is a Atlassian Stash and the branch is set as the default, you will get an error "By default, deleting the current branch is denied...". I had to change the default branch in Stash to point to another branch before I could delete.
  • user1021726
    user1021726 over 9 years
    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).
  • Seng Cheong
    Seng Cheong over 9 years
    Note that -D forces the deletion. It's always better to use -d, which will remind if you need to do something dangerous.
  • Felipe
    Felipe about 9 years
    ahahah :) it's up to you: use -d if you want to see git crying or -D if you want to cry.
  • Reishin
    Reishin almost 9 years
    sorry, but install Ruby for that kind of work ? More logical is implementation on bash, which will work out of box.
  • Dan Rosenstark
    Dan Rosenstark almost 9 years
    @Reishin Ruby is installed on the box just like Bash, at least on OSX. Please see: stackoverflow.com/questions/2342894/…, where this topic has been discarded as opinion-based by SO.
  • Reishin
    Reishin almost 9 years
    @Yar this link is out of the context and have a more broader scope. I tell only about git and as topic is not originated only for OSX, that choose is strange for other systems (e.g. *UNIX, Windows)
  • Trevor Boyd Smith
    Trevor Boyd Smith almost 9 years
    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.
  • Sandra
    Sandra over 8 years
    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.ht‌​ml
  • eMBee
    eMBee over 8 years
    git push --prune origin didn't do anything for me on gitlab: git clone git://repo.git; git branch -d -r origin/some-branches; git push --prune origin; yields: Everything up-to-date; git fetch; brings locally deleted branches back; git push --mirror; now they are really gone!
  • mahemoff
    mahemoff over 8 years
    This is perfectly simple as you've done it, but fyi git also lets you make custom commands. Put git push origin --delete $1 in a file on your path called git-shoot and git shoot branchname will work too.
  • Neelabh Singh
    Neelabh Singh over 8 years
    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'.
  • Daemedeor
    Daemedeor over 8 years
    I didn't downvote, but my thinking is that it isn't substantively helping. The question is obviously asking for a more commandline type answer without having to use an external program, if people were clicking into here, they likely won't be looking for a github for desktop ways.
  • Eric
    Eric over 8 years
    @Daemedeor , I dissagree. In 2010 when the OP asked the question, the UI way of doing it didn't exist and the only option was command line. To indicate that you want a command line only option it should be stated in the question or with the tag, command-line-interface, which in this case is no present.
  • rooby
    rooby over 8 years
    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.
  • Samitha Chathuranga
    Samitha Chathuranga about 8 years
    Does the remote branch deletion requires "git push" afterwards ?
  • huggie
    huggie about 8 years
    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?
  • Admin
    Admin about 8 years
    @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).
  • Kellen Stuart
    Kellen Stuart about 8 years
    I had to run git branch -D Branch_Name to get rid of the local branch
  • vezenkov
    vezenkov about 8 years
    The git command for deleting a remote branch sucks and I tend to forget it (both new and old). Luckily there are GUI tools that have the option. Git Gui, TortoiseGit and GitHub Desktop have it - I wish Git Extensions had this functionality too. Anyway, what I remember is to start Git Gui from within Git Extensions when I need to delete a remote branch.
  • BTRUE
    BTRUE about 8 years
    @KolobCanyon You only have to use -D if the branch has not been merged into another branch.
  • code_dredd
    code_dredd about 8 years
    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.
  • Dhanuka777
    Dhanuka777 almost 8 years
    If your branch in your fork (not always origin), use the proper repository. e.g. git push myRepo :mrbranchtodelete
  • jww
    jww almost 8 years
    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...
  • kunl
    kunl almost 8 years
    expanding on this, --delete "$@" and -D "$@" instead of $1 will handle it for multiple branches.
  • Kermit_ice_tea
    Kermit_ice_tea almost 8 years
    This works if its your own branch. But if you are pruning all unneeded branches in the repo (some of which aren't yours) it wouldn't suffice
  • bryn
    bryn almost 8 years
    I suggest running git branch -d (with lowercase 'd') first to ensure changes have been merged, and then push if successful (put && in between commands)
  • Eric
    Eric about 7 years
    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
  • Alwin Kesler
    Alwin Kesler about 7 years
    Be careful with de -D option. In a batch consider using lower -d
  • Jared Knipp
    Jared Knipp about 7 years
    In my case, I'm almost always deleting after merging (or without the need to merge). Using lower -d will require the branch be merged before deleting, using -D forces the branch deletion.
  • caesarsol
    caesarsol about 7 years
    ⚠️ Use git branch -D carefully in a script, since it force-deletes a branch without checking it has been merged. Use -d to be safe.
  • Michał Szajbe
    Michał Szajbe almost 7 years
    @SamithaChathuranga no, git push origin :<branchname> already pushes an 'empty` branch to the remote (hence deletes the remote branch)
  • Kermit_ice_tea
    Kermit_ice_tea almost 7 years
    +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.
  • LoranceChen
    LoranceChen almost 7 years
    git branch -a will display local and remote branches.It will be help for you diagram introduce.
  • RBT
    RBT almost 7 years
    I don't see these Overview, Yours, Active, State and All branches tab on GitHub website. Looks like this is an old snapshot. Under the Code tab, I see sub-tabs like commits, branches, releases and contributors. When I am the owner of a repository then I see an additional tab named MIT.
  • Alex
    Alex over 6 years
    To better understand the git push origin :<branch_name> syntax, go to stackoverflow.com/questions/1519006/…
  • Sheldon
    Sheldon over 6 years
    Note that the -d as an alias for --delete also works with the push call. So git push origin -d <branch_name> works too
  • Sheldon
    Sheldon over 6 years
    git branch --delete <branch> doesn't delete a remote branch, you need git push <server> --delete <branch> to do that
  • Alex78191
    Alex78191 over 6 years
    Why do I need to specify remote when pushing?
  • Philip Rego
    Philip Rego over 6 years
    @Alex78191 Because you may not want to push your branch to all your remotes. However I use this alias when I want to push all my branch to all my remotes. stackoverflow.com/a/18674313/1308453
  • cst1992
    cst1992 over 6 years
    ...and safer to use :O
  • luator
    luator over 6 years
    @Sheldon Which git version are you using? For me (git 2.7.4) -d is not working for push.
  • Sheldon
    Sheldon over 6 years
    @luator I am using 2.8.1, and my apologies, -d as an alias for --delete was added in 2.8.0, see here: legacy-developer.atlassian.com/blog/2016/03/…
  • ZakJ
    ZakJ over 6 years
    I needed to use --delete instead of -d to delete remote branch.
  • Vivek Maru
    Vivek Maru over 6 years
    -d option is an alias for --delete and if --delete work then -d should also work, if you forcefully want to delete a branch you can use -D instead of -d or --delete.
  • Amit Dash
    Amit Dash about 6 years
    You forgot the part about deleting the local branch which can be done by: git branch -d <local_branch> or git branch -D <local_branch> for force deleting
  • user1101733
    user1101733 about 6 years
    You can use source tree tool as well if you don't want to remember git commands.
  • Christophe Roussy
    Christophe Roussy almost 6 years
    Only $ git push <remote_name> :<branch_name> worked for me.
  • BaDr Amer
    BaDr Amer almost 6 years
    note that if you are setting on the branch you want to delete, you need to checkout a branch other than the one you need to delete (eg: master) before deleting the local branch.
  • Alois Mahdal
    Alois Mahdal almost 6 years
    The --delete might be easier to remember, but it's too late if you already understand the push remote :deadbr, plus the cute thing about the latter is that it also works for tags.
  • Caltor
    Caltor over 5 years
    In the executive summary you have git push --delete <remote_name> <branch_name> but in the first "Delete Remote Branch Update" section you have git push <remote_name> --delete <branch_name>. i.e. The parameters are in a different order. Can I suggest making these the same to avoid confusion please.
  • Dani
    Dani over 5 years
    I ran the first line of the executive summary and the second wasn't "needed"(?). The branch is gone both locally and remotely.
  • Timmmm
    Timmmm over 5 years
    @DanielSpringer That shouldn't be the case. It will delete the remote branch origin/mybranch in your repo and on the server, but it won't delete any local tracking branches (mybranch).
  • Dani
    Dani over 5 years
    @Timmmm either I ran it before and forgot, or... I don’t know :P
  • stwr667
    stwr667 about 5 years
    Note that this can fail with ! [remote rejected] <branch_name> (refusing to delete the current branch: refs/heads/<branch_name>) error: failed to push some refs to '[email protected]:<org_name>/<project_name>.git' if you have set this branch in Github as your default branch. If you have, go to your project in Github -> "Settings" -> "Branches" and then change your Default Branch to another. Then repeating your command to delete the branch will work.
  • vibs2006
    vibs2006 almost 5 years
    When branches get deleted on origin, your local repository won't take notice of that. You'll still have your locally cached versions of those branches (which is actually good) but git branch -a will still list them as remote branches. You can clean up that information locally like this: git remote prune origin Your local copies of deleted branches are not removed by this. The same effect is achieved by using git fetch --prune
  • jmiserez
    jmiserez over 4 years
    The image is distracting and very large and adds nothing to the answer. I hope this does not become a trend on SO.
  • Robin A. Meade
    Robin A. Meade over 4 years
    This answer shows how to delete a branch on a specific remote. For consistency, the prune step should also be limited to that specific remote. Drop the --all option and specify the remote: git fetch --prune <remote_name>.
  • Mogens TrasherDK
    Mogens TrasherDK over 4 years
    Installing Python to do something git does well by itself, is not really a solution. More like an programming exercise.
  • Eugene Yarmash
    Eugene Yarmash over 4 years
    @Mogens Python is already preinstalled in most sane distributions. With git only you can't for example: 1) customize the output (e.g. make it more consistent) 2) combine multiple commands in desired way 3) easily customize the logic. Besides, entering the same commands over and over again is pretty boring.
  • David P
    David P about 4 years
    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.
  • SebMa
    SebMa about 4 years
    @Eric-Brotto For your answer to be complete, you need to add the git fetch -p command to fetch changes on other machines
  • Gabriel Staples
    Gabriel Staples about 4 years
    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!
  • Andy Macleod
    Andy Macleod almost 4 years
    This can be piped into other commands like grep to filter out branches you do not want to delete git branch --merged | grep -v "\$BRANCHNAME\b" | xargs git branch -d
  • Charming Robot
    Charming Robot almost 4 years
    > well -D force deletes, -d gives you a warning if it's not already merged in. \n\n Pretty bad naming if you ask me. Should be -df or -d --force.
  • Timo
    Timo over 3 years
    I just wanted to remove an entry from the git remote -v list (I correct: 2 entries, namely the upstream ones) and came across this post. Well my solution is to remove the remote branch from .git/config editing this file. I added them by accident with git remote add ..
  • Honghao Zhang
    Honghao Zhang over 3 years
    I really have no idea why git command is so inconsistent and unintuitive to remember. Looks at the deletion, one is -D, another one is -d|--delete
  • Jean-François Fabre
    Jean-François Fabre over 3 years
    you must disclose affiliation when linking to external resources you're the author of.
  • Gabriel Staples
    Gabriel Staples over 3 years
    What happens if you run git fetch --prune, withOUT specifying the remote branch? Does it prune all remotes?
  • Gabriel Staples
    Gabriel Staples over 3 years
    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.
  • Dror Bar
    Dror Bar over 3 years
    If the repository is deleted and you need to delete the local branch tracking the remote branch, use git branch -d -r <remote>/<branch>
  • Dror Bar
    Dror Bar over 3 years
    If the repository is deleted and you need to delete the local branch tracking the remote branch, use git branch -d -r <remote>/<branch>
  • a.ak
    a.ak about 3 years
    Is it necessary to push changes after deleting a remote branch?
  • Timo
    Timo about 3 years
    More on the : and why it deletes
  • Timo
    Timo about 3 years
    I get still the same error error: failed to push some refs to 'https://github.com/tik9/tik9.github.io' when git push origin :refs/heads/main. Probably github is the culprit.
  • Duncan MacIntyre
    Duncan MacIntyre almost 3 years
    Great answer—I appreciate how the visuals explain why! You could consider making the "short answer" even shorter (e.g. list only the short versions of commands, choose one of -d and -D and note the other as per @Eric Brotto). The alternate forms can follow below.
  • Andras Deak -- Слава Україні
    Andras Deak -- Слава Україні almost 3 years
    Did git push <remote_name> :<branch_name> really not work? In your case git push origin :Test_FE. That's listed in the top-voted, accepted answer which was posted 11 years ago.
  • Andras Deak -- Слава Україні
    Andras Deak -- Слава Україні almost 3 years
    Sorry, that should probably have been :feature/Test_FE.
  • sachyy
    sachyy almost 3 years
    yes. may be because of the directories it did not work. With correct naming it should work as I can see the number of upvotes. But I thought of sharing what I learnt. Thanks
  • C-Dev
    C-Dev over 2 years
    -d does not work for me. The terminal tell me to use -D instead
  • Robo Robok
    Robo Robok over 2 years
    Why the f would git push origin :foo delete a branch? GIT doesn't stop surprising me with its poor interface conventions.
  • Oleg Valter is with Ukraine
    Oleg Valter is with Ukraine over 2 years
    @RoboRobok historical reasons - since the refspec is structured as source:target, supplying an empty source is functionally the same as deleting something. Even they knew this is not a very idiomatic way of deleting something, thus we have an alias of git push --delete for convenience, but functionally it is still the same.
  • vitaliis
    vitaliis about 2 years
    Why do you delete remote branch in executive summary first?
  • lylhw13
    lylhw13 almost 2 years
    In git version 2.25.1, git branch also has -d, --delete option. -D is shortcut for --delete --force.
  • Timo
    Timo almost 2 years
    It does not delete the remote branch, I get remote: ! Push rejected, cannot delete main branch
  • Bob
    Bob almost 2 years
    The executive summary was spot on!