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

$ 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 push -d origin <branch-name>

git push origin :<branch-name>

- 也可以用此语法删除标记

强制删除本地分支

git branch -D <branch-name>

注:do 做 agit fetch --all --prune删除远程分支后, 在其它机器上删除已过时的跟踪分支 。

示例示例示例示例

要删除本地分支

git branch -D my-local-branch

要删除远程分支

git push origin :my-remote-branch

随着新版本的Git, 也有可能删除分支

git push origin --delete <branch_name>

TIP :如果您想要查看所有可用的分支, 您可以使用git branch -a,

并且只看到边远的树枝, 你可以使用git branch -r

其他回答

执行前执行前

git branch --delete <branch>

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

git ls-remote

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

提示:当您使用删除分支时

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.

最灵活的方式是使用自定义 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 命令的最新版本在这里欢迎提出意见和建议。

另一种办法是:

git push --prune origin

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

git push --mirror

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

git push origin --delete <branch Name>

较容易记住

git push origin :branchName