Delete a Git branch both locally and remotely

4
  

Original question: Delete a Git branch both locally and remotely by Matthew Rankin

I want to delete a branch both locally and at the branch of my remote project on Github.

Deleted local branch successfully

$ git branch -D bugfix
Deleted branch bugfix (was 2a14ef7).

Note: The -D option is a shortcut of --delete --force . If you want to remove the local branch that must be fully merged in the upper branches, use -d which is a shortcut of -delete .

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 do I need to do differently to successfully remove the remotes / origin / bugfix branch both locally and on Github?

    
asked by Guilherme Bussi Dias 23.12.2015 в 23:00
source

1 answer

6
  

Original reply: Delete a Git branch both locally and remotely    by Matthew Rankin

Response updated on Feb 1, 2012

Starting with Git v1.7.0 , you can delete one Remote branch using:

git push origin --delete <NombreRama>

which is easier to remember than

git push origin :<NombreRama>

that was added in Git v1.5.0 "to remove a remote branch or a tag ".

Consequently, the version of git you have installed will impose if you need to use the easiest or most difficult syntax.

Original answer of 5-Jan-2010

From chapter 3 of Pro Git by Scott Chacon:

  

Delete remote branches

     

Suppose you have finished a remote branch - you and your collaborators have   finished with a feature and you have merged it in your   remote master branches (or any branch where your code is stable)   this). You can delete a remote branch using instead of the   obtuse syntax git push [NombreRemoto] :[Rama] . If you want to remove   your the serverfix branch of the server, do the following:

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

You no longer have the branch on your server.

     

As there is a great possibility that you forget the syntax, you could   want to remember this command later. One way to remember this   command is calling the syntax git push [NombreRemoto][RamaLocal]:[RamaRemota] that we mentioned before. If you do not put the part    [localbranch] , then you're basically saying, "Do not take anything in   my part and do it [remotebranch] ".

I used git push origin :bugfix and it worked very well. Scott Chacon was right - I would bookmark that page (or do it here, responding in Stack Overflow).

    
answered by 23.12.2015 в 23:00