How do I delete a Git branch locally and remotely?

后端 未结 30 3678
被撕碎了的回忆
被撕碎了的回忆 2020-11-21 04:11

I want to delete a branch both locally and remotely.

Failed Attempts to Delete a Remote Branch

$ git branch -d         


        
相关标签:
30条回答
  • 2020-11-21 04:45

    To delete your branch locally and remotely

    • Checkout to master branch - git checkout master

    • Delete your remote branch - git push origin --delete <branch-name>

    • Delete your local branch - git branch --delete <branch-name>

    0 讨论(0)
  • 2020-11-21 04:45

    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
    
    0 讨论(0)
  • 2020-11-21 04:46

    You can also do this using git remote prune origin

    $ git remote prune origin
    Pruning origin
    URL: git@example.com/yourrepo.git
     * [pruned] origin/some-branchs
    

    It prunes and deletes remote-tracking branches from a git branch -r listing.

    0 讨论(0)
  • 2020-11-21 04:47

    Since January 2013, GitHub included a Delete branch button next to each branch in your "Branches" page.

    Relevant blog post: Create and delete branches

    0 讨论(0)
  • 2020-11-21 04:50

    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.

    0 讨论(0)
  • 2020-11-21 04:50

    This won't work if you have a tag with the same name as the branch on the remote:

    $ git push origin :branch-or-tag-name
    error: dst refspec branch-or-tag-name matches more than one.
    error: failed to push some refs to 'git@github.com:SomeName/some-repo.git'
    

    In that case you need to specify that you want to delete the branch, not the tag:

    git push origin :refs/heads/branch-or-tag-name
    

    Similarly, to delete the tag instead of the branch you would use:

    git push origin :refs/tags/branch-or-tag-name
    
    0 讨论(0)
提交回复
热议问题