How do I delete a Git branch locally and remotely?

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

    To delete locally - (normal)

    git branch -d my_branch
    

    If your branch is in a rebasing/merging progress and that was not done properly, it 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 you need to solve the 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
    

    You can do the same using:

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

    Graphical representation:

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

    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 deleting the branch without checking merged status)

    To remove a remote branch from the server:

    git push origin --delete {the_remote_branch}

    Reference: Git: Delete a branch (local or remote)

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

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

    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 second way as it is more intuitive.

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

    Simply say:

    git branch -d <branch-name>
    git push origin :<branch-name>
    
    0 讨论(0)
  • 2020-11-21 05:09
    git push origin --delete <branch Name>
    

    is easier to remember than

    git push origin :branchName
    
    0 讨论(0)
提交回复
热议问题