JavaEar 专注于收集分享传播有价值的技术资料

How do I delete a Git branch both locally and remotely?

I want to delete a branch both locally and on my remote project fork on GitHub.

Failed Attempts to Delete 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
* [new branch] bugfix -> origin/bugfix
Already up-to-date.

What do I need to do differently to successfully delete the remotes/origin/bugfix branch both locally and on GitHub?


  1. If you want to complete both these steps with a single command, you can make an alias for it by adding the below to your ~/.gitconfig:

        rmbranch = "!f(){ git branch -d ${1} && git push origin --delete ${1}; };f"

    Alternatively, you can add this to your global config from the command line using

    git config --global alias.rmbranch \
    '!f(){ git branch -d ${1} && git push origin --delete ${1}; };f'

    NOTE: If using -d (lowercase d), the branch will only be deleted if it has been merged. To force the delete to happen, you will need to use -D (uppercase D).

  2. 参考答案2
  3. Delete locally:

    To delete a local branch, you can use:

    git branch -d branch_name 

    To delete a branch forcibly, use -D instead of -d.

    git branch -D branch_name

    Delete remotely:

    There are two options:

    git push origin :branchname  
    git push origin --delete branchname 

    I would suggest you use the 2nd way as it is more intuitive.

  4. 参考答案3
  5. You can also do this using git remote prune origin:

    $ git remote prune origin
    Pruning origin
     * [pruned] origin/some-branchs

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

  6. 参考答案4
  7. 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>

  8. 参考答案5
  9. In addition to the other answers, I often use the git_remote_branch tool. It's an extra install, but it gets you a convenient way to interact with remote branches. In this case, to delete:

    grb delete branch

    I find that I also use the publish and track commands quite often.

  10. 参考答案6
  11. 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
  12. 参考答案7
  13. Delete remote branch

    git push origin :<branchname>

    Delete local branch

    git branch -D <branchname>

    Delete local branch steps:

    1. checkout to another branch
    2. delete local branch
  14. 参考答案8
  15. One liner command delete both local, and remote:

    D=branch-name; git branch -D $D; git push origin :$D

    or add the alias below to your ~/.gitconfig; usage: git kill branch-name

        kill = "!f(){ git branch -D \"$1\";  git push origin --delete \"$1\"; };f"
  16. 参考答案9
  17. Simply say:

    git branch -d <branch-name>
    git push origin :<branch-name>
  18. 参考答案10
  19. 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.

  20. 参考答案11
  21. Now you can do it with the GitHub Desktop app.

    After launching the app

    1. Click on the project containing the branch
    2. Switch to the branch you would like to delete switching branch
    3. From the "Branch" menu, select, "Unpublish...", to have the branch deleted from the GitHub servers. unpublish branch
    4. From the "Branch" menu, select, 'Delete "branch_name"...', to have the branch deleted off of your local machine (aka the machine you are currently working on) delete local branch
  22. 参考答案12
  23. git push origin --delete branchName

    is easier to remember than

    git push origin :branchName
  24. 参考答案13
  25. To delete Locally - (Normal),

    git branch -d my_branch

    If your branch in rebasing/merging progress and that was not done properly means, you will get an error Rebase/Merge in progress so in that case, you won't be able to delete your branch.

    So either your need to solve rebasing/merging otherwise you can do force Delete by using,

    git branch -D my_branch

    To delete in Remote:

    git push --delete origin my_branch

    can do the same using ,

    git push origin :my_branch   # easy to remember both will do the same.

    Graphical Representation,

    enter image description here

  26. 参考答案14
  27. 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 ''

    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
  28. 参考答案15
  29. According to latest document using terminal we can delete following way.

    delete in local:

    git branch -D usermanagement

    delete in remote location:

    git push --delete origin usermanagement
  30. 参考答案16
  31. Many of the other answers will lead to errors/warnings. This approach is relatively fool proof although you may still need git branch -D branch_to_delete if it's not fully merged into some_other_branch, for example.

    git checkout some_other_branch
    git push origin :branch_to_delete
    git branch -d branch_to_delete

    Remote pruning isn't needed if you deleted the remote branch. It's only used to get the most up to date remotes available on a repo you're tracking. I've observed git fetch will add remotes, not remove them. Here's an example of when git remote prune origin will actually do something:

    User A does the steps above. User B would run the following commands to see the most up to date remote branches

    git fetch
    git remote prune origin
    git branch -r
  32. 参考答案17
  33. Steps for deleting a branch:

    for deleting the remote branch:

    git push origin --delete <your_branch> 

    for deleting the local branch:

    git branch -D <branch_name> 

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

    Simply do git push origin --delete to delete your remote branch, 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 also can 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 branch in git

  34. 参考答案18
  35. I got sick of googling for this answer, so I took a similar approach to the answer that crizCraig posted earlier.

    Added the following to my Bash profile:

    function gitdelete(){
        git push origin --delete $1
        git branch -D $1

    Then every time I'm done with a branch (merged into master, for example) I run the following in my terminal:

    gitdelete my-branch-name

    ...which then deletes my-branch-name from origin as as well as locally.

  36. 参考答案19
  37. Mashup of all the other answers. Requires Ruby 1.9.3+, tested only on OS X.

    Call this file git-remove, make it executable, and put it in your path. Then use, for example, git remove temp.

    #!/usr/bin/env ruby
    require 'io/console'
    if __FILE__ == $0
          branch_name = ARGV[0] if (ARGV[0])
          print "Press Y to force delete local and remote branch #{branch_name}..."
        response = STDIN.getch
        if ['Y', 'y', 'yes'].include?(response)
          puts "\nContinuing."
          `git branch -D #{branch_name}`
          `git branch -D -r origin/#{branch_name}`
          `git push origin --delete #{branch_name}` 
          puts "\nQuitting."
  38. 参考答案20
  39. git push origin :bugfix  # Deletes remote branch
    git branch -d bugfix     # Must delete local branch manually

    If you are sure you want to delete it, run

    git branch -D bugfix

    Now to clean up deleted remote branches run

    git remote prune origin
  40. 参考答案21
  41. If you want to delete a branch, first checkout to the branch other than the branch to be deleted.

    git checkout other_than_branch_to_be_deleted

    Deleting the local branch:

    git branch -D branch_to_be_deleted

    Deleting the remote branch:

    git push origin --delete branch_to_be_deleted
  42. 参考答案22
  43. Before executing

    git branch --delete <branch>

    make sure you determine first what the EXACT name of the remote branch is by executing:

    git ls-remote

    This will tell you what to enter EXACTLY for <branch> value. (branch is case sensitive!)

  44. 参考答案23
  45. Tip: When you delete branches using

    git branch -d <branchname>    # deletes local branch


    git push origin :<branchname> # deletes remote branch

    only the references are deleted. Even though the branch is actually removed on the remote the references to it still exists in the local repositories of your team members. This means that for other team members the deleted branches are still visible when they do a git branch -a.

    To solve this your team members can prune the deleted branches with

    git remote prune <repository>

    This is typically git remote prune origin.

  46. 参考答案24
  47. I added the following aliases to my .gitconfig file. This allows me to delete branches with or without specifying the branch name. Branch name is defaulted to the current branch if no argument is passed in.

        branch-name = rev-parse --abbrev-ref HEAD     
        rm-remote-branch = !"f() { branch=${1-$(git branch-name)}; git push origin :$branch; }; f"
        rm-local-branch = !"f() { branch=${1-$(git branch-name)}; git checkout master; git branch -d $branch; }; f"
        rm-branch-fully = !"f() { branch=${1-$(git branch-name)}; git rm-local-branch $branch; git rm-remote-branch $branch; }; f"
  48. 参考答案25
  49. I created the following function:

        if [[ -n $1 ]]; then
            git checkout master > /dev/null;
            echo "Deleting local $branch_name branch...";
            git branch -D "$branch_name";
            echo "Deleting remote $branch_name branch...";
            git push origin --delete "$branch_name";
            git remote prune origin;
            echo "Your current branches are:";
            git branch -a;
            echo "Usage: git-delete-branch <branch_name>";
  50. 参考答案26
  51. 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 deletion without checking merged status)

    To remove a remote branch from the server:

    git push origin --delete {the_remote_branch}


  52. 参考答案27
  53. git branch -D <name-of-branch>
    git branch -D -r origin/<name-of-branch>
    git push origin :<name-of-branch>
  54. 参考答案28
  55. I also had similar issues, and this seems to work: This deletes local branch. git branch -d the_local_branch

    This removes remote branch git push origin :the_remote_branch

    Source: Makandra Cards

  56. 参考答案29
  57. An alternative option to the command line for deleting remote branches is the GitHub branches page.

    See for example:

    Found in the Code -> Branches page of a GitHub repository.

    I generally prefer command line myself but this GitHub page shows you lots more information about the branches, such as last updated date and user, and number of commits ahead and behind. It is useful when dealing with a large number of branches.

  58. 参考答案30
  59. I use the following in my Bash settings:

    alias git-shoot="git push origin --delete"

    Then you can call:

    git-shoot branchname
  60. 参考答案31
  61. This is simple: Just run the following command:

    To delete a Git branch both locally and remotely, first delete the local branch using command:

    git branch -d example

    (here example is the branch name)

    And after that delete remote branch using command:

    git push origin :example
  62. 参考答案32
  63. There are good answer, but, in case that you have a ton of branches, deleting them one by one locally and remotly, would be a tedious tasks. You can use this script to automate this tasks.

    branch_not_delete=( "master" "develop" "our-branch-1" "our-branch-2")
    for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do
        # delete prefix remotes/origin/ from branch name
        branch_name="$(awk '{gsub("remotes/origin/", "");print}' <<< $branch)"
        if ! [[ " ${branch_not_delete[*]} " == *" $branch_name "* ]]; then
            # delete branch remotly and locally
            git push origin :$branch_name
    • List the branches that you don't want to delete
    • Iterate over remotes branch and if they aren't in our "preserve list", we deleted.

    Source: removing-git-branches-at-once

  64. 参考答案33
  65. Another approach is

    git push --prune origin

    WARNING: This will delete all remote branches that do not exist locally. Or more comprehensively,

    git push --mirror

    will effectively make the remote repository look like the local copy of the repository (local heads, remotes and tags are mirrored on remote).

  66. 参考答案34
  67. Executive Summary

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

    Delete Local Branch

    To delete the local branch use one of the following:

    $ git branch -d branch_name
    $ git branch -D branch_name

    Note: The -d option is an alias for --delete, which only deletes the branch if it has already been fully merged in its upstream branch. You could also use -D, which is an alias for --delete --force, which deletes the branch "irrespective of its merged status." [Source: man git-branch]

    Delete Remote Branch [Updated on 8-Sep-2017]

    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 on 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 master branch (or whatever branch your stable codeline is in). You can delete a remote branch using the rather obtuse syntax git push [remotename] :[branch]. If you want to delete your serverfix branch from the server, you run the following:

    $ git push origin :serverfix
     - [deleted]         serverfix

    Boom. No more branch 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

    git fetch --all --prune

    to propagate changes.

  68. 参考答案35
  69. 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 :<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 tracking branches
    git fetch <remote> -p      # Shorter

    The Long Answer: there are 3 different branches to delete!

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

    Note that deleting the remote branch X from the command line using a git push will also delete 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, though 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 repo 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 --multipleoptions, 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 --remote or -r flags:

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

    See Also

  70. 参考答案36
  71. I use the following in my Bash settings:

    alias git-shoot="git push origin --delete"

    Then you can call:

    git-shoot branchname
  72. 参考答案37
  73. Using GitBash you can execute the following:

    git branch --delete <branch>


    From the GitHub desktop app, when you have the branch checked out, you can delete the Local branch via the Branch menu strip:

    enter image description here

    If you ARE NOT using the GitHub desktop app, and are using an IDE like Visual Studio for your local source control, all you have to do is a couple quick steps:

    1. Check out a branch other than the one you wish to delete.
    2. Right-click the branch you wish to delete.
    3. Select the Delete option from the context menu.

    Then, once logged in to your GitHub account online, go to the repository and click the All Branches tab. From there, just click the little trash can icon on the right on the name of the brach you wish to delete.

    enter image description here

    *Keep in mind - if the branch isn't published, there's no need to try to delete it from your online repository.

  74. 参考答案38
  75. Since January 2013, GitHub included a Delete branch button next to each branch in your "Branches" page.

    Relevant blog post: Create and delete branches

  76. 参考答案39
  77. This commands works best for me:

    git branch -D <branch name>