首页 文章

如何在本地和远程删除Git分支?

提问于
浏览
14365

我想在本地和GitHub上的远程项目分支上删除分支 .

尝试删除远程分支失败

$ 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
From github.com:gituser/gitproject
* [new branch] bugfix -> origin/bugfix
Already up-to-date.

在本地和GitHub上成功删除 remotes/origin/bugfix 分支需要做些什么?

30 回答

  • 214

    另一种方法是

    git push --prune origin
    

    WARNING: 这将删除本地不存在的所有远程分支 . 或者更全面,

    git push --mirror
    

    将有效地使远程存储库看起来像存储库的本地副本(本地磁头,远程和标签在远程镜像) .

  • 48

    除了其他答案,我经常使用git_remote_branch工具 . 这是一个额外的安装,但它为您提供了一种与远程分支机构进行交互的便捷方式 . 在这种情况下,要删除:

    grb delete branch
    

    我发现我也经常使用 publishtrack 命令 .

  • 82

    在执行之前

    git branch --delete <branch>
    

    确保首先通过执行确定远程分支的确切名称:

    git ls-remote
    

    这将告诉您输入 <branch> 值的确切内容 . ( branch 区分大小写!)

  • 180

    执行摘要

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

    请注意,在大多数情况下,远程名称是 origin .

    删除本地分支

    要删除本地分支,请使用以下某个选项:

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

    Note: -d 选项是 --delete 的别名,如果已在其上游分支中完全合并,则仅删除分支 . 你也可以使用 -D ,它是 --delete --force 的别名,删除分支"irrespective of its merged status." [来源: man git-branch ]

    删除远程分支[2017年9月8日更新]

    Git v1.7.0开始,您可以使用删除 remote 分支

    $ git push <remote_name> --delete <branch_name>
    

    这可能比记住更容易记住

    $ git push <remote_name> :<branch_name>
    

    Git v1.5.0 "to delete a remote branch or a tag."中添加了

    Git v2.8.0开始,您还可以将 git push-d 选项一起用作 --delete 的别名 .

    因此,您安装的Git版本将决定您是否需要使用更简单或更难的语法 .

    删除远程分支[原始答案自2010年1月5日起]

    来自Scott Chacon的Pro Git的第3章:

    删除远程分支假设您已完成远程分支 - 例如,您和您的协作者已完成某项功能并已将其合并到您的远程主分支(或您的稳定代码行所在的任何分支) . 您可以使用相当钝的语法git push [remotename]:[branch]删除远程分支 . 如果要从服务器中删除serverfix分支,请运行以下命令:$ git push origin:serverfix
    致git@github.com:schacon/simplegit.git

    • [删除] serverfix
      繁荣 . 您的服务器上没有更多分支 . 您可能想要熟悉此页面,因为您需要该命令,并且您可能会忘记语法 . 记住这个命令的一种方法是回忆一下我们之前讨论过的git push [remotename] [localbranch]:[remotebranch]语法 . 如果你放弃[localbranch]部分,那么你基本上就是说:“不要采取任何行动,让它成为[remotebranch] . ”

    我发布了 git push origin :bugfix ,它的工作非常精彩 . Scott Chacon是对的 - 我想要dog ear那个页面(或者通过在Stack Overflow上回答这个问题,实际上是狗耳朵) .

    然后你应该在其他机器上执行它

    git fetch --all --prune
    

    传播变化 .

  • 119

    删除分支的步骤:

    删除 remote branch:

    git push origin --delete <your_branch>
    

    删除 local branch ,有三种方法:

    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: 好的,请解释一下这里发生了什么!

    只需做 git push origin --deletedelete your remote branch ONLY ,在末尾添加分支的名称,这将删除并同时将其推送到远程...

    另外, git branch -D ,它只是删除了本地分支 ONLY !...

    -D 代表 --delete --force ,它将删除分支,即使它没有合并(强制删除),但你也可以使用 -d 代表 --delete ,它会抛出分支合并状态的错误...

    我还创建了下面的图像来显示步骤:

  • 67
    git branch -D <name-of-branch>
    git branch -D -r origin/<name-of-branch>
    git push origin :<name-of-branch>
    
  • 84

    简短答案

    如果您想要更详细地解释以下命令,请参阅下一节中的长答案 .

    删除远程分支:

    git push origin --delete <branch>  # Git version 1.7.0 or newer
    git push origin :<branch>          # Git versions older than 1.7.0
    

    删除本地分支:

    git branch --delete <branch>
    git branch -d <branch> # Shorter version
    git branch -D <branch> # Force delete un-merged branches
    

    删除本地远程跟踪分支:

    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:有3个不同的分支要删除!

    当您处理本地和远程删除分支时,请记住 there are 3 different branches involved

    • 本地分支 X .

    • 远程原点分支 X .

    • 跟踪远程分支 X 的本地远程跟踪分支 origin/X .

    Visualization of 3 branches

    使用的原始海报

    git branch -rd origin/bugfix
    

    其中只删除了 local remote-tracking branch origin/bugfix ,而不是 origin 上的实际远程分支 bugfix .

    Diagram 2

    To delete that actual remote branch ,你需要

    git push origin --delete bugfix
    

    Diagram 3

    其他细节

    以下部分介绍了删除远程和远程跟踪分支时要考虑的其他详细信息 .

    推送删除远程分支也会删除远程跟踪分支

    请注意,使用 git push will also delete the local remote-tracking branch origin/X 从命令行删除远程分支 X ,因此没有必要使用 git fetch --prunegit fetch -p 修剪过时的远程跟踪分支,尽管它如果你这样做也不会受伤 .

    您可以通过运行以下命令验证是否还删除了远程跟踪分支 origin/X

    # 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
    

    修剪过时的本地远程跟踪分支origin / X.

    如果您没有从命令行删除远程分支 X (如上所述),那么您的本地仓库仍将包含(现在已过时)远程跟踪分支 origin/X . 例如,如果您通过GitHub的Web界面直接删除了远程分支,就会发生这种情况 .

    删除这些过时的远程跟踪分支(从Git版本1.6.6开始)的典型方法是使用 --prune 或更短的 -p 运行 git fetch . 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
    

    以下是1.6.6 release notes(强调我的)的相关引用:

    “git fetch”学习--all和--multipleoptions,从许多存储库运行提取,以及--prune选项删除过时的远程跟踪分支 . 这些使得“git remote update”和“git remote prune”不那么必要(虽然没有计划删除“远程更新”或“远程修剪”) .

    替代以上自动修剪过时的远程跟踪分支

    或者,不是通过 git fetch -p 修剪过时的本地远程跟踪分支,而是通过手动删除带有 --remote-r 标志的分支来避免进行额外的网络操作:

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

    另请参阅

  • 85

    我厌倦了谷歌搜索这个答案,所以我采取了类似的方法早于the answer that crizCraig posted .

    在我的Bash配置文件中添加了以下内容:

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

    然后每当我完成一个分支(例如合并到 master )时,我在终端中运行以下命令:

    gitdelete my-branch-name
    

    ...然后从 origin 以及本地删除 my-branch-name .

  • 43

    提示:使用时删除分支

    git branch -d <branchname>    # deletes local branch
    

    要么

    git push origin :<branchname> # deletes remote branch
    

    只删除引用 . 即使分支在遥控器上实际被删除,对它的引用仍然存在于团队成员的本地存储库中 . 这意味着对于其他团队成员,删除的分支在执行 git branch -a 时仍然可见 .

    要解决此问题,您的团队成员可以修剪已删除的分支

    git remote prune <repository>
    

    这通常是 git remote prune origin .

  • 105

    您还可以使用以下命令删除远程分支 .

    git push --delete origin serverfix
    

    这与做同样的事情

    git push origin :serverfix
    

    但它可能更容易记住 .

  • 71

    Deleting Branches

    让我们假设我们在分支“联系表单”上的工作已经完成,我们已经将它集成到“master”中 . 由于我们不再需要它,我们可以删除它(本地):$ git branch -d contact-form

    并删除远程分支:

    git push origin --delete contact-form
    
  • 111

    To delete Locally - (Normal),

    git branch -d my_branch
    

    如果您的分支在重新定位/合并进度并且未正确完成意味着,您将收到错误 Rebase/Merge in progress 所以在这种情况下,您将无法删除您的分支 .

    所以要么你需要解决变基/合并,否则你可以通过使用强制删除,

    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,

  • 961
    git push origin :bugfix  # Deletes remote branch
    git branch -d bugfix     # Must delete local branch manually
    

    如果您确定要删除它,请运行

    git branch -D bugfix
    

    现在清理已删除的远程分支运行

    git remote prune origin
    
  • 60

    在本地和远程删除分支

    • 结帐到主分公司 - git checkout master

    • 删除远程分支 - git push origin --delete <branch-name>

    • 删除您当地的分行 - git branch --delete <branch-name>

  • 47

    如果要使用单个命令完成这两个步骤,可以通过将以下内容添加到 ~/.gitconfig 来为其创建别名:

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

    或者,您可以使用命令行将其添加到全局配置中

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

    NOTE :如果使用 -d (小写d),则仅在合并时才删除分支 . 要强制删除,您需要使用 -D (大写D) .

  • 249

    如果要删除分支,请首先签出除要删除的分支以外的分支 .

    git checkout other_than_branch_to_be_deleted
    

    删除本地分支:

    git branch -D branch_to_be_deleted
    

    删除远程分支:

    git push origin --delete branch_to_be_deleted
    
  • 2941

    自2013年1月起,GitHub在"Branches"页面的每个分支旁边都包含一个删除分支按钮 .

    相关博文:Create and delete branches

  • 76

    One liner 命令删除本地和远程:

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

    或者将下面的别名添加到〜/ .gitconfig;用法: git kill branch-name

    [alias]
        kill = "!f(){ git branch -D \"$1\";  git push origin --delete \"$1\"; };f"
    
  • 17981

    如果您的标签与远程分支的名称相同,则无效:

    $ 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'
    

    在这种情况下,您需要指定要删除分支,而不是标记:

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

    同样,要删除标记而不是分支,您将使用:

    git push origin :refs/tags/branch-or-tag-name
    
  • 102

    删除远程分支

    git push origin :<branchname>

    删除本地分支

    git branch -D <branchname>

    删除本地分支步骤:

    • 结账到另一个分行

    • 删除本地分支

  • 1695

    这很简单:只需运行以下命令:

    要在本地和远程删除Git分支,首先使用以下命令删除本地分支:

    git branch -d example
    

    (这里 example 是分支名称)

    然后使用命令删除远程分支:

    git push origin :example
    
  • 718

    您也可以使用 git remote prune origin 执行此操作:

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

    它从 git branch -r 列表中修剪和删除远程跟踪分支 .

  • 337

    马修的答案非常适合删除远程分支机构我也很欣赏这个解释,但要简单区分两个命令:

    要从您的机器中删除 local branch

    git branch -d {the_local_branch} (使用 -D 代替强制删除分支而不检查合并状态)

    要从服务器中删除 remote branch

    git push origin --delete {the_remote_branch}

    参考:https://makandracards.com/makandra/621-git-delete-a-branch-local-or-remote

  • 151

    现在你可以使用GitHub Desktop应用程序来完成它 .

    启动应用程序后

    • 单击包含分支的项目

    • 切换到要删除的分支

    • 从"Branch"菜单中,选择"Unpublish...",以从GitHub服务器中删除分支 .

    • 从"Branch"菜单中,选择“删除”branch_name“...”,将分支从本地计算机(也就是您当前正在处理的计算机)中删除

  • 96

    许多其他答案将导致错误/警告 . 这种方法相对简单,但如果它没有完全合并到_207524中,你可能仍然需要 git branch -D branch_to_delete .

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

    远程修剪不仅仅是用于获取最新的遥控器,你可以使用're tracking. I'观察 git fetch 将添加遥控器,而不是删除它们 . 这是 git remote prune origin 实际上会做什么的一个例子:

    用户A执行上述步骤 . 用户B将运行以下命令以查看最新的远程分支

    git fetch
    git remote prune origin
    git branch -r
    
  • 94

    我在Bash设置中使用以下内容:

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

    然后你可以打电话:

    git-shoot branchname
    
  • 80
    git push origin --delete branchName
    

    更容易记住

    git push origin :branchName
    
  • 44

    Delete locally:

    要删除本地分支,您可以使用:

    git branch -d branch_name
    

    要强制删除分支,请使用 -D 而不是 -d .

    git branch -D branch_name
    

    Delete remotely:

    有两种选择:

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

    我建议你使用第二种方式,因为它更直观 .

  • 349

    简单地说:

    git branch -d <branch-name>
    git push origin :<branch-name>
    
  • 41

    所有其他答案的混搭 . 需要Ruby 1.9.3,在OS X上测试 only .

    调用此文件 git-remove ,使其可执行,并将其放在您的路径中 . 然后使用,例如 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}` 
        else
          puts "\nQuitting."
        end
    end
    

相关问题