How do I delete a Git branch locally and remotely?

后端 未结 30 3591
被撕碎了的回忆
被撕碎了的回忆 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:57

    Executive Summary

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

    Note that in most cases the remote name is origin. In such a case you'll have to use the command like so.

    $ git push -d origin <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 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 git@github.com: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.

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

    A one-liner command to 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

    [alias]
        kill = "!f(){ git branch -D \"$1\";  git push origin --delete \"$1\"; };f"
    
    0 讨论(0)
  • 2020-11-21 04:59

    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
    0 讨论(0)
  • 2020-11-21 05:01
    git branch -D <name-of-branch>
    git branch -D -r origin/<name-of-branch>
    git push origin :<name-of-branch>
    
    0 讨论(0)
  • 2020-11-21 05:01

    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).

    0 讨论(0)
  • 2020-11-21 05:03

    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 repository 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
    
    0 讨论(0)
提交回复
热议问题