删除远程分支的尝试失败 :

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

我如何正确删除本地和远程的远程/原产/bugfix分支?


当前回答

我厌倦了勾引这个答案, 所以我采取了类似的方法 来回答克里格早些时候发布的答案。

我在我的巴什档案中增加了以下内容:

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

每当我完成一个分支(例如, 合并为主人)时, 我在我的终端里运行以下功能:

gitdelete my-branch-name

...然后从源头和本地删除我的处名

其他回答

内容提要

git push -d <remote_name> <branchname>
git branch -d <branchname>

注:在大多数情况下,<remote_name>将是来源。

删除本地分处

删除本地分支时使用下列分支之一:

git branch -d <branch_name>
git branch -D <branch_name>

-d 选项是 --delete的别名,它只删除已经完全合并到上游分支的分支。 -D选项是 -- delete-force的别名,它删除分支“不管其合并状态如何”。 [资料来源: man git-branch] As of Git v2.3, git 分支 -d (删除) 学会尊重 -f (力量) 旗帜。如果试图删除当前选中的分支, 你会收到错误 。

删除远程分支

至 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 开始,您也可以使用 - d 选项的 Git 推动作为 -- delette 的别名。 因此, 您安装的 Git 版本将决定您是否需要使用更简单或更难的语法 。

删除远程处[原件:2010年1月5日的答复]

Scott Chacon在《Pro Git》第三章中写道:

删除远程分支 假设您已经用远程分支完成 — — 比如, 您和您的合作者已经完成了一个功能, 并已将其合并到您的远程主要分支( 或您稳定的代码行所在的分支 ) 。 您可以使用非常隐蔽的 usyntatget git 推 [ remotename] 来删除远程分支 : [branch] 。 如果您想要从服务器中删除服务器上的服务器链接分支, 您将运行以下操作 :

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

boom 。 服务器上不再有树枝。 您可能想要在这个页面上下手, 因为您需要这个命令, 您可能会忘记语法 。 记住这个命令的一个方法就是回顾我们稍早一些时段的语法。 如果您离开[ 本地支 部分, 那么您基本上会说 : “ 不要在我身边做任何事情,让它成为[远程支 。 ”

斯科特·查孔说得没错,我想在Stack Overflow上回答这个话题(或几乎是狗耳朵 ) 。

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

# Fetch changes from all remotes and locally delete 
# remote deleted branches/tags etc
# --prune will do the job :-;
git fetch --all --prune

以传播变化。

最灵活的方式是使用自定义的 Git 命令。 例如, 在您的 $PATH 中以 Git- rmbranch 命名的某个地方创建以下 Python 脚本, 并使其可以执行 :

#!/usr/bin/env python3

import argparse
import subprocess
import sys

def rmbranch(branch_name, remote, force):
    try:
        print(subprocess.run(['git', 'branch', '-D' if force else '-d', branch_name],
                             capture_output=True, check=True, encoding='utf-8').stdout, end='')
    except subprocess.CalledProcessError as exc:
        print(exc.stderr.replace(f'git branch -D {branch_name}', f'git rmbranch -f {branch_name}'), end='')
        return exc.returncode

    return subprocess.run(['git', 'push', remote, '--delete', branch_name]).returncode    

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Delete a Git branch locally and remotely.')
    parser.add_argument('-r', '--remote', default='origin', help="The remote name (defaults to 'origin')")
    parser.add_argument('-f', '--force', action='store_true', help='Force deletion of not fully merged branches')
    parser.add_argument('branch_name', help='The branch name')
    args = parser.parse_args()

    sys.exit(rmbranch(args.branch_name, args.remote, args.force))

Git Rmbranch -h 将展示您使用信息 :

usage: git-rmbranch [-h] [-r REMOTE] [-f] branch_name

Delete a Git branch locally and remotely.

positional arguments:
  branch_name           The branch name

optional arguments:
  -h, --help            show this help message and exit
  -r REMOTE, --remote REMOTE
                        The remote name (defaults to 'origin')
  -f, --force           Force deletion of not fully merged branches

请注意, git 推进源 - delete <branch_ name > 也会删除本地远程跟踪分支( 默认情况下为源/ < branch_ name > ) , 因此不需要关注 。

P. S. 您可以在此找到这个 Git 命令的最新版本, 欢迎提出意见和建议 。

本地删除 :

要删除本地分支,请使用:

git branch -d <branch_name>

强行删除分支,使用 -D 而不是 -d 。

git branch -D <branch_name>

远程删除 :

有两个选项:

git push origin :branchname

git push origin --delete branchname

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

这是所有其他答案的拼图。 它需要 Ruby 1.9. 3 +, 并且只在 OS X 上测试 。

调用此文件 git- remove, 让它可执行, 并把它放在您的路径中。 然后使用, 例如, git 删除临时 。

#!/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

如果您想要删除分支, 请先向要删除的分支以外的分支首选 。

git checkout other_than_branch_to_be_deleted

删除本地分支 :

git branch -D branch_to_be_deleted

删除远程分支 :

git push origin --delete branch_to_be_deleted