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

$ 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 检查退出母版母版 删除您的远程分支 - git 推进源 - delette <branch- name > 删除您的本地分支 - git 分支 - delette < branch- name >

其他回答

如果您想要用一个命令来完成这两个步骤, 您可以在 ~ /. gitconfig 中添加下面的替代名称 :

[alias]
    rmbranch = "!f(){ git branch -d ${1} && git push origin --delete ${1}; };f"

或者,您可以从命令行使用

git config --global alias.rmbranch \
'!f(){ git branch -d ${1} && git push origin --delete ${1}; };f'

注:如果使用 -d(小写d),该分支只有在合并时才被删除。要强制删除,您需要使用 -D(大写D)。

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

您可以在此删除与 Glob 或任何分支名称相对应的远程分支 :

git branch -r --list "origin/*" | xargs git branch -r -D

删除远程分支

git 推推源 : <branchname>

删除本地分支

git 分支 -D <branchname>

删除本地分支步骤 :

退出退出到另一个分支删除本地分支

本地和远程删除您的分支

退出到母版分支 - git 检查退出母版母版 删除您的远程分支 - git 推进源 - delette <branch- name > 删除您的本地分支 - git 分支 - delette < branch- name >