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

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

git push origin :<branchname> # Deletes remote branch

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

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

git remote prune <repository>

这是典型的git remote prune origin.

其他回答

我厌倦了勾勾勾勾勾勾勾勾勾勾勾的答案, 所以我采取了类似的办法,克雷格所发回的答案早些时候。

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

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

每当我完成一个分支(合并成master例如)我在我的终端运行以下功能:

gitdelete my-branch-name

...然后删除my-branch-name调自origin以及当地。

使用吉特巴什您可以执行以下操作:

git branch --delete <branch>

-

从 GitHub 桌面应用程序中删除当地当地通过分支通过分处菜单条 :

Enter image description here

如果你(如果)不属于使用 GitHub 桌面应用程序, 并正在使用像视觉工作室那样的 IDE 来控制本地源码, 您只需要快速的一步 :

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

然后,一旦登录到您的 GitHub 账户, 一旦登录到您的 GitHub 在线账户, 转到存储库并单击所有各处选项卡。从此选项卡中,单击您想要删除的分支名称右侧的小垃圾桶图标。

Enter image description here

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

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

git checkout other_than_branch_to_be_deleted

删除本地分支 :

git branch -D branch_to_be_deleted

删除远程分支 :

git push origin --delete branch_to_be_deleted

执行前执行前

git branch --delete <branch>

确保您首先确定精确远程分支的名称是执行 :

git ls-remote

这将告诉您要进入什么精确用于<branch>值 。 () (branch案件敏感! )

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

#!/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 push origin --delete <branch_name>并删除本地远程跟踪分支(origin/<branch_name>))(因此不必在意这一点。

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