How do I delete a Git branch locally and remotely?

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

    Now you can do it with the GitHub Desktop application.

    After launching the application

    1. Click on the project containing the branch
    2. Switch to the branch you would like to delete

    3. From the "Branch" menu, select, "Unpublish...", to have the branch deleted from the GitHub servers.

    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)

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

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

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

    Use:

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

    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:

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

    Alternatively, you can add this to your global configuration 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).

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

    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.

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

    Steps for deleting a branch:

    For deleting the remote branch:

    git push origin --delete <your_branch>
    

    For deleting the local branch, you have three ways:

    1: git branch -D <branch_name>
    
    2: git branch --delete --force <branch_name>  # Same as -D
    
    3: git branch --delete  <branch_name>         # Error on unmerge
    

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

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

    0 讨论(0)
提交回复
热议问题