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

$ 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 branch -d <branchname> # Deletes local branch

git push origin :<branchname> # Deletes remote branch

仅删除引用 。 即使该分支实际上被远程删除, 但它的引用仍然存在于您的团队成员所在的本地存储库中 。 这意味着对于其他团队成员来说, 被删除的分支在做 Git 分支 - a 时仍然可见 。

为了解决这个问题,你的团队成员可以使用

git remote prune <repository>

这是典型的 Git 远程纯素来源 。

其他回答

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

grb delete branch

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

执行前执行前

git branch --delete <branch>

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

git ls-remote

这将指示您为 < branch> 值输入什么 。 ( branch 是区分大小写 ! )

删除远程分支的命令行的替代选项是 GitHub 分支页面。

例如,见:https://github.com/agorm/agorgon.js/branches。

在代码 - > GitHub 库的分支页面中找到 。

我一般更喜欢指挥行本身,但这个 GitHub 页面显示您对分支的更多信息,例如最新日期和用户,以及前后承诺的数量。 这对于处理大量分支很有用。

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

git push origin :serverfix

但它可能更容易记住。