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

问题描述 投票:15395回答:38

我想在本地和远程删除分支。

尝试删除远程分支失败

$ 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分支?

git git-branch git-remote
38个回答
19433
投票

Executive Summary

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

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

Delete Local Branch

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

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

注意:-d选项是--delete的别名,如果已经在其上游分支中完全合并,则只删除分支。您也可以使用-D,它是--delete --force的别名,它会删除分支“无论其合并状态如何”。 [来源:man git-branch]

Delete Remote Branch [Updated on 8-Sep-2017]

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选项作为--delete的别名。

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

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

来自Scott Chacon的Pro Git第3章:

Deleting Remote Branches

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

$ git push origin :serverfix
To [email protected]:schacon/simplegit.git
 - [deleted]         serverfix

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

我发布了git push origin :bugfix,它工作得很漂亮。斯科特查孔是对的 - 我想要dog ear那个页面(或者通过在Stack Overflow上回答这个问题,实际上是狗耳朵)。

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

git fetch --all --prune

传播变化。


194
投票

另一种方法是: -

git push --prune origin

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

git push --mirror

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


163
投票

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

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

然后你可以打电话:

git-shoot branchname

128
投票

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

相关博文:Create and delete branches


125
投票

在本地删除:

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

git branch -d <branch_name> 

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

git branch -D <branch_name>

远程删除:

有两种选择:

git push origin :branchname  

git push origin --delete branchname 

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


122
投票

如果您想使用单个命令完成这两个步骤,可以通过将以下内容添加到~/.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)。


113
投票

在本地和远程删除分支

  • 结帐到主分公司 - git checkout master
  • 删除你的远程分支 - git push origin --delete <branch-name>
  • 删除您当地的分支 - git branch --delete <branch-name>

109
投票

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

$ git remote prune origin
Pruning origin
URL: [email protected]/yourrepo.git
 * [pruned] origin/some-branchs

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


104
投票

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

grb delete branch

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


93
投票

一个衬管命令删除本地和远程:

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"

91
投票

Deleting Branches

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

$ git branch -d contact-form

并删除远程分支:

git push origin --delete contact-form

3060
投票

Matthew的答案很适合删除远程分支,我也很欣赏这个解释,但是要简单区分这两个命令:

要从您的计算机中删除本地分支:

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


86
投票

删除远程分支

git push origin :<branchname>

删除本地分支

git branch -D <branchname>

删除本地分支步骤:

  1. 结账到另一个分行
  2. 删除本地分支

85
投票

简单地说:

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

81
投票
git push origin --delete <branch Name>

更容易记住

git push origin :branchName

79
投票

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

启动应用程序后

  1. 单击包含分支的项目
  2. 切换到您要删除的分支switching branch
  3. 从“分支”菜单中,选择“取消发布...”,以从GitHub服务器中删除分支。 unpublish branch
  4. 从“分支”菜单中选择“删除”branch_name“...”,将分支从本地计算机(也就是您当前正在处理的计算机)中删除delete local branch

73
投票

要删除本地 - (正常),

git branch -d my_branch

如果您的分支在重新定位/合并进度并且未正确完成意味着,您将收到错误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


65
投票

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

$ 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 '[email protected]:SomeName/some-repo.git'

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

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

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

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

51
投票

许多其他答案都会导致错误/警告。这种方法相对简单,虽然你可能仍然需要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

49
投票

我厌倦了谷歌搜索这个答案,所以我早些时候对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


45
投票

在执行之前

git branch --delete <branch>

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

git ls-remote

这将告诉您什么输入<branch>值。 (branch区分大小写!)


44
投票
git push origin :bugfix  # Deletes remote branch
git branch -d bugfix     # Must delete local branch manually

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

git branch -D bugfix

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

git remote prune origin

1768
投票

The Short Answers

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

删除远程分支:

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: there are 3 different branches to delete!

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

  1. 当地分公司X
  2. 远程起源分支X
  3. 跟踪远程分支origin/X的本地远程跟踪分支X

使用的原始海报

git branch -rd origin/bugfix

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

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

git push origin --delete bugfix

Additional Details

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

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

请注意,使用X从命令行删除远程分支git push也将删除本地远程跟踪分支origin/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开始)的典型方法是使用git fetch或更短的--prune运行-p。请注意,这将删除远程不再存在的任何远程分支的所有过时的本地远程跟踪分支:

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

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

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

Alternative to above automatic pruning for obsolete remote-tracking branches

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

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

也可以看看


43
投票

所有其他答案的混搭。需要Ruby 1.9.3+,仅在OS 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

1360
投票

Steps for deleting a branch:

要删除远程分支:

git push origin --delete <your_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

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

只需要git push origin --delete删除你的远程分支,最后添加分支的名称,这将删除并同时将其推送到远程...

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

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

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

delete a remote and local branch in git


756
投票

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

git push --delete origin serverfix

这与做同样的事情

git push origin :serverfix

但它可能更容易记住。


362
投票

提示:使用时删除分支

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


360
投票

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

git checkout other_than_branch_to_be_deleted

删除本地分支:

git branch -D branch_to_be_deleted

删除远程分支:

git push origin --delete branch_to_be_deleted

260
投票
git branch -D <name-of-branch>
git branch -D -r origin/<name-of-branch>
git push origin :<name-of-branch>

221
投票

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

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

git branch -d example

(这里example是分支名称)

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

git push origin :example
© www.soinside.com 2019 - 2024. All rights reserved.