代码之家  ›  专栏  ›  技术社区  ›  Matthew Rankin val

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

  •  14807
  • Matthew Rankin val  · 技术社区  · 15 年前

    我想在本地和远程项目fork上删除一个分支 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.
    

    要成功删除 remotes/origin/bugfix 在本地和Github上都进行分支?

    39 回复  |  直到 15 年前
        1
  •  18509
  •   Death-is-the-real-truth    6 年前

    执行摘要

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

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

    删除本地分支

    删除 地方的 分支使用下列之一:

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

    注: 这个 -d 选项是的别名 --delete ,仅当分支已在其上游分支中完全合并时才删除该分支。你也可以用 -D ,它是的别名 --delete --force ,删除分支“无论其合并状态如何。”[来源: man git-branch ]

    删除远程分支【2017年9月8日更新】

    至于 Git v1.7.0 ,您可以删除 遥远的 分支使用

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

    这可能比

    $ git push <remote_name> :<branch_name>
    

    添加到 Git v1.5.0 “删除远程分支或标记。”

    开始于 Git v2.8.0 您也可以使用 git push -D 选项作为的别名 --删除 .

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

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

    摘自第3章 Pro Git 作者:Scott Chacon:

    删除远程分支

    假设你完成了一个远程分支,比如说,你和你的合作者完成了一个功能,并将它合并到你的远程主分支(或者你的稳定代码行所在的任何分支)中。您可以使用相当钝的语法删除远程分支 git push [remotename] :[branch] . 如果要从服务器中删除服务器修复分支,请运行以下操作:

    $ git push origin :serverfix
    To git@github.com:schacon/simplegit.git
     - [deleted]         serverfix
    

    繁荣。服务器上不再有分支。您可能希望仔细阅读本页,因为您需要该命令,而且您可能会忘记语法。记住此命令的一种方法是调用 git push [remotename] [localbranch]:[remotebranch] 我们之前讨论过的语法。如果你离开 [localbranch] 部分,那么你基本上是在说,不要拿任何东西在我身边,让它成为 [remotebranch] 艾斯

    我发布 git push origin :bugfix 而且效果很好。Scott Chacon是对的,我想 dog ear 那个页面(或者通过在堆栈溢出时回答这个问题而实际上是狗耳朵)。

    那么你应该在其他机器上执行这个

    git fetch --all --prune
    

    传播更改。

        2
  •  2993
  •   Rin Minase    7 年前

    马修的回答很好地消除了 遥远的 Branch和我也很欣赏这一解释,但为了简单地区分这两个命令:

    删除一个 本地分支 从您的机器:

    git branch -d {the_local_branch} (使用) -D 而是在不检查合并状态的情况下强制删除分支)

    删除一个 远程分支 从服务器:

    git push origin --delete {the_remote_branch}

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

        3
  •  1723
  •   Valentin Lorentz    9 年前

    简短的回答

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

    删除远程分支:

    git push origin--delete<branch>git version 1.7.0或更新版本
    Git Push原始版本:<Branch>Git版本早于1.7.0
    

    删除本地分支:

    git branch--delete<branch>
    Git Branch-D<Branch>较短版本
    git branch-d<branch>强制删除未合并的分支
    < /代码> 
    
    

    删除本地远程跟踪分支:.

    git branch--delete--remotes<remote>/<branch>
    git branch-dr<remote>/<branch>较短
    
    git fetch<remote>--删除多个过时的跟踪分支
    git fetch<远程>-p更短
    

    答案很长:有3个不同的分支要删除!

    当您处理本地和远程删除分支时,请记住,涉及3个不同的分支

    1. 本地分支x
    2. 远程原点分支x
    3. 跟踪远程分支的本地远程跟踪分支origin/x

    使用的原始海报

    git branch-rd origin/bugfix
    < /代码> 
    
    

    这只删除了他的本地远程跟踪分支origin/bugfix,而不是实际的远程分支bugfixonorigin

    要删除实际的远程分支,您需要

    git push origin--delete bugfix
    

    其他详细信息

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

    按删除远程分支也会删除远程跟踪分支

    请注意,使用命令行中的git push从命令行中删除远程分支x,也会删除本地远程跟踪分支origin/x,因此不必使用git fetch--prune修剪过时的远程跟踪分支不管怎样,如果你这样做了会很受伤。

    您可以通过运行以下命令来验证远程跟踪分支origin/x是否也被删除:

    Git分支--远程
    列出远程分支
    
    #严格查看本地和远程跟踪分支
    Git分支--全部
    查看所有的分支
    < /代码> 
    
    

    修剪过时的本地远程跟踪分支源站/x

    如果没有从命令行中删除远程分支x(如上所述),则本地repo仍将包含(现在已过时的)远程跟踪分支origin/x。例如,如果直接通过Github的Web界面删除远程分支,就会发生这种情况。

    删除这些过时的远程跟踪分支(自Git 1.6.6版以来)的一个典型方法是简单地运行Git fetchwith the——pruneor shorter-p请注意,这会删除远程上不再存在的任何远程分支的所有过时本地远程跟踪分支

    git fetch origin--prune
    Git获取原点-P更短
    < /代码> 
    
    

    以下是1.6.6发行说明中的相关引述(emphasis mine):。

    < Buff行情>

    “git fetch”learned——alland——multipleoptions,to run fetch from 许多存储库,以及删除远程跟踪的选项 过时的分支。这些分支使“git remote update”和“git 远程修剪“不太必要”(没有删除“远程”的计划) 不过,更新“nor”或“remote prune”)。

    < /块引用>

    对于过时的远程跟踪分支,替代上述自动修剪

    或者,不必通过git fetch-p,而通过,修剪过时的本地远程跟踪分支,您可以避免进行额外的网络操作,只需使用——remote手动删除分支即可

    git branch--delete--remotes origin/x
    Git分支-Dr Origin/X更短
    < /代码> 
    
    

    另请参见

    下面的命令,然后在下一节中看到长答案。

    删除远程分支:

    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
    

    答案很长:有3个不同的分支要删除!

    当您处理本地和远程删除分支时,请记住涉及3个不同的分支机构:

    1. 当地分支机构X.
    2. 远程起源分支X.
    3. 本地远程跟踪分支origin/X跟踪远程分支X.

    Visualization of 3 branches

    使用的原始海报

    git branch -rd origin/bugfix
    

    只删除了他的本地远程跟踪分支origin/bugfix而不是实际的远程分支bugfixorigin.

    Diagram 2

    删除实际的远程分支,你需要

    git push origin --delete bugfix
    

    Diagram 3

    其他详细信息

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

    按删除远程分支也会删除远程跟踪分支

    请注意,删除远程分支X从命令行使用git push还将删除本地远程跟踪分支原点/X,因此不必使用git fetch --prunegit fetch -p但不管怎样,如果你做了也不会有什么伤害。

    您可以验证远程跟踪分支原点/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
    

    修剪过时的本地远程跟踪分支源站/x

    如果不删除远程分支X从命令行(如上所述),那么本地repo仍将包含(现在已过时的)远程跟踪分支起源/ X. 例如,如果直接通过Github的Web界面删除远程分支,则可能发生这种情况。

    删除这些过时的远程跟踪分支(因为Git版本1.6.6)的一个典型方法是简单地运行git fetch--prune或更短-p.请注意,这将删除远程上不再存在的任何远程分支的所有过时本地远程跟踪分支。:

    git fetch origin --prune
    git fetch origin -p # Shorter
    

    这是1.6.6 release notes(重点是我的):

    “Git Fetch”学习--all--multiple选项,从中运行提取 许多存储库,以及--修剪删除远程跟踪的选项 枯枝。这使得“git远程更新”和“git 远程修剪“不太必要”(没有删除“远程”的计划) 不过,更新“nor”remote prune)。

    对于过时的远程跟踪分支,替代上述自动修剪

    或者,不要通过删除过时的本地远程跟踪分支GIT-FETCH-P,您可以避免进行额外的网络操作只需用--remote-r旗帜:

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

    也见

        4
  •  1137
  •   Alireza    6 年前

    删除分支的步骤:

    删除远程分支

    git push origin--删除<您的分支>
    < /代码> 
    
    

    对于删除本地分支,您有三种方法

    1:git branch-d<branch_name>
    
    2:git branch--delete--force<branch ou name>//与-d相同
    
    3:git branch--删除<branch_name>//取消合并时出错
    < /代码> 
    
    

    解释:好的,解释一下这里发生了什么!

    只需执行git push origin--deletetodelete your remote branch only,在末尾添加分支的名称,这将删除并同时将其推送到remote…。

    另外,git branch-d,这只会删除本地分支only!…

    -d表示即使分支没有合并,也会删除它(force delete),但您也可以使用-dwhich表示--deletewhich throw an error respondent of the branch merge status…。

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

    用于删除本地分支你有三路:

    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
    

    说明:好的,解释一下这里发生了什么!

    简单地做git push origin --delete仅删除远程分支,在末尾添加分支的名称,这将同时删除并将其推送到远程…

    也,git branch -D,只需删除本地分支只有你说什么?……

    -D代表--delete --force即使分支未合并,也会删除它(强制删除),但您也可以使用-d代表--delete这会引发一个错误,分别是分支合并状态…

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

    delete a remote and local branch in git

        5
  •  741
  •   Death-is-the-real-truth    6 年前

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

    git push --delete origin serverfix
    

    哪个做的和

    git push origin :serverfix
    

    但它可能更容易记住。

        6
  •  355
  •   willeM_ Van Onsem    10 年前

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

    git checkout other_than_branch_to_be_deleted
    

    删除本地分支:

    git branch -D branch_to_be_deleted
    

    删除远程分支:

    git push origin --delete branch_to_be_deleted
    
        7
  •  349
  •   Death-is-the-real-truth    6 年前

    提示:删除分支时使用

    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 .

        8
  •  256
  •   Felipe    11 年前
    git branch -D <name-of-branch>
    git branch -D -r origin/<name-of-branch>
    git push origin :<name-of-branch>
    
        9
  •  220
  •   willeM_ Van Onsem    10 年前

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

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

    git branch -d example
    

    (这里 example 是分支名称)

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

    git push origin :example
    
        10
  •  190
  •   Death-is-the-real-truth    6 年前

    另一种方法是:

    git push --prune origin
    

    警告: 这将删除本地不存在的所有远程分支。 或者更全面地说,

    git push --mirror
    

    将有效地使远程存储库看起来像存储库的本地副本(本地头、远程设备和标记在远程设备上镜像)。

        11
  •  159
  •   Death-is-the-real-truth    6 年前

    我在我的 Bash 设置:

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

    然后你可以打电话:

    git-shoot branchname
    
        12
  •  127
  •   Death-is-the-real-truth    6 年前

    自2013年1月起,Github包括 删除分支 “分支”页面中每个分支旁边的按钮。

    相关博客帖子: Create and delete branches

        13
  •  119
  •   Ryan Kohn uday    11 年前

    如果您想用一个命令完成这两个步骤,可以通过将下面的添加到您的 ~/.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'
    

    注释 如果使用 -d (小写d),仅当分支已合并时才会删除。要强制删除,您需要使用 -D (大写D)。

        14
  •  115
  •   Death-is-the-real-truth    6 年前

    本地删除:

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

    git branch -d <branch_name> 
    

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

    git branch -D <branch_name>
    

    远程删除:

    有两种选择:

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

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

        15
  •  112
  •   dippas    7 年前

    本地和远程删除分支

    • 向主分行结账- git checkout master

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

    • 删除本地分支- git branch --delete <branch-name>

        16
  •  105
  •   Death-is-the-real-truth    6 年前

    您也可以使用 git remote prune origin

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

    它从 git branch -r 上市。

        17
  •  101
  •   Death-is-the-real-truth    6 年前

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

    grb delete branch
    

    我发现我也使用 publish track 命令非常频繁

        18
  •  91
  •   Vinnie James    8 年前

    一班轮 命令删除 本地和远程 :

    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"
    
        19
  •  90
  •   Ulysses Alves    10 年前

    Deleting Branches

    假设分支“联系人表单”的工作已经完成,并且我们已经将其集成到“master”中。因为我们不再需要它,我们可以删除它(本地):

    $ git branch -d contact-form
    

    删除远程分支:

    git push origin --delete contact-form
    
        20
  •  86
  •   jayxhj    9 年前

    删除远程分支

    git push origin :<branchname>

    删除本地分支

    git branch -D <branchname>

    删除本地分支步骤:

    1. 签出到另一个分支
    2. 删除本地分支
        21
  •  85
  •   Peter Mortensen icecrime    9 年前

    简单地说:

    git branch -d <branch-name>
    git push origin :<branch-name>
    
        22
  •  79
  •   Eric Val    8 年前

    现在您可以使用github desktop

    启动应用程序后

    1. 单击包含分支的项目
    2. 切换到要删除的分支
    3. 从“Branch”菜单中,选择“Unpublish…”,将分支从GitHub服务器中删除。
    4. 从“branch”(分支)菜单中,选择“delete” branch_name “…”,将分支从本地计算机(即当前正在使用的计算机)中删除
    应用程序

    1. 单击包含分支的项目
    2. 切换到要删除的分支 switching branch
    3. 从“Branch”菜单中,选择“Unpublish…”,将分支从GitHub服务器中删除。 unpublish branch
    4. 从“Branch”(分支)菜单中,选择“Delete”(删除)。 分支名称 “…”,将分支从本地计算机(即当前正在使用的计算机)中删除。 delete local branch
        23
  •  77
  •   Death-is-the-real-truth    6 年前
    git push origin --delete <branch Name>
    

    比…更容易记住

    git push origin :branchName
    
        24
  •  74
  •   Alex Weitz Shailendra Singh    8 年前

    to delete locally-(normal),

    git branch-d my_branch
    < /代码> 
    
    

    如果您的分支处于重新平衡/合并进程中,而这未正确完成,则意味着您将收到一个错误rebase/merge in progressso在这种情况下,您将无法删除您的分支。

    因此,要么您需要解决重新平衡/合并问题,要么您可以使用强制删除,

    git branch-d my_branch
    < /代码> 
    
    

    要在远程删除:

    git push--delete origin my_branch
    < /代码> 
    
    

    可以用同样的方法来做,

    git push origin:my_branch easy to remember both will do the same.
    < /代码> 
    
    

    图形表示,

    食人魔,如果做得不好,意味着你会出错。Rebase/Merge in progress因此,在这种情况下,您将无法删除您的分支。

    因此,要么您需要解决重新平衡/合并问题,要么您可以使用强制删除,

    git branch -D my_branch
    

    要在远程删除:

    git push --delete origin my_branch
    

    可以用同样的方法,

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

    图形表示,

    enter image description here

        25
  •  63
  •   Death-is-the-real-truth    6 年前

    如果您有一个与远程分支同名的标记,则此操作将不起作用:

    $ 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
    
        26
  •  49
  •   Community CDub    8 年前

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

    将以下内容添加到我的bash配置文件中:

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

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

    gitdelete my-branch-name
    

    …然后删除 my-branch-name origin 以及当地。

        27
  •  49
  •   Death-is-the-real-truth    6 年前

    许多其他答案将导致错误/警告。这种方法是相对简单的,尽管你可能仍然需要 git branch -D branch_to_delete 如果没有完全合并到 some_other_branch 例如。

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

    如果删除了远程分支,则不需要进行远程修剪。它只用于获取您正在跟踪的存储库中可用的最新远程设备。我已经观察到了 git fetch 将添加遥控器,而不是删除它们。下面是一个例子 git remote prune origin 实际上会做些什么:

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

    git fetch
    git remote prune origin
    git branch -r
    
        28
  •  45
  •   Peter Mortensen icecrime    9 年前

    执行前

    git branch --delete <branch>
    

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

    git ls-remote
    

    这将告诉您输入的确切目的 <branch> 价值。( branch 区分大小写!)

        29
  •  44
  •   user    9 年前
    git push origin :bugfix  # Deletes remote branch
    git branch -d bugfix     # Must delete local branch manually
    

    如果确定要删除,请运行

    git branch -D bugfix
    

    现在要清除已删除的远程分支,请运行

    git remote prune origin
    
        30
  •  42
  •   Death-is-the-real-truth    6 年前

    把所有其他答案混为一谈。要求 Ruby 1.9.3+ 测试 只有 关于操作系统X。

    调用此文件 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
    
    推荐文章