GitHub Tips: Removing a Remote Branch

If you are working on a branch and want to abandon it, what do you do?

It’s easy to remove it from your local repository:

$ git branch -d mybranch # delete branch "mybranch"

And then how do you remove it from a GitHub repository? This works:


$ git push git@github.com:<my_account>/<my_repository>.git :heads/<mybranch>

But there’s an easier way. Scott Chacon suggests:

You can also do (assuming ‘origin’ is the name of your remote):

git push origin :mybranch

Scott Chacon is the author of the Git Internals book from Peepcode.

Advertisements

7 Responses to GitHub Tips: Removing a Remote Branch

  1. Scott Chacon says:

    You can also do (assuming ‘origin’ is the name of your remote) :

    git push origin :mybranch

    Which is the same syntax as pushing a branch ref there, except with a leading ‘:’ on the branch name – it just looks more complex to put the whole repo url there instead of your remote name, and the ‘heads/’ prefix shouldn’t be necessary.

  2. Gingah says:

    Scott Chacon is perfectly right in the seconds syntax, but when for example you have a tag with an identical name as a branch (which I did), I had to use the first method (and it worked perfectly).

  3. mitchwd says:

    I had an error (remote: error: refusing to delete the current branch) deleting a branch using Scott’s code, but found that setting another branch as the default and then retrying worked!
    Github > Repository Admin > Default Branch > [Other branch]

    (As explained here: http://superuser.com/questions/294407/cant-remove-remote-branch-in-git)

    Hope this might help other people!

  4. lianglee says:

    Thanks this work for me

  5. Chris says:

    This did exactly what I need – thanks for posting.

  6. spuder says:

    Thanks for the post, git push origin :someBranch worked great for me

  7. This did exactly what I need – thanks for posting.
    definetly bookmarked!

%d bloggers like this: