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

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


当前回答

最灵活的方式是使用自定义的 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 push --prune origin

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

git push --mirror

将有效地使远程存储库看起来像本地版本的存储库(本地负责人、远程和标签在远程镜像) 。

除了其他答案之外, 我经常使用 git_ remote_ branch 工具。 这是一个额外的安装, 但是它能方便地与远程分支进行互动。 在这种情况下, 请删除 :

grb delete branch

我发现我还经常使用 出版和跟踪命令。

git push origin --delete <branch Name>

较容易记住

git push origin :branchName

自2013年1月以来, GitHub 在您的“ Branches” 页中, 在每个分支旁边有一个删除分支按钮 。

相关博客文章: 创建和删除分支

使用 Git Bash 您可以执行以下操作 :

git branch --delete <branch>

- 带

从 GitHub 桌面应用程序中, 当您检查了分支时, 您可以通过分支菜单条删除本地分支 :

如果您没有使用 GitHub 桌面应用程序, 并且正在使用像视觉工作室那样的 IDE 来控制本地源码, 您只需要采取几个快速步骤 :

检查您要删除的分支以外的分支。右键单击您想要删除的分支。从上下文菜单中选择删除选项。

然后,一旦在网上登录到您的 GitHub 账户, 转到仓库, 并单击所有分支的标签。 从那里, 单击小垃圾桶就可以在右侧您想要删除的分支的名称上图标 。

无需试图从你的网上存储库删除。