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

$ 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 <branch_name>

强行删除分支,使用-D代替-d.

git branch -D <branch_name>

远程删除 :

有两个选项:

git push origin :branchname

git push origin --delete branchname

我建议你用第二种方式,因为它更直观。

其他回答

A A A一班班要删除的命令本地和偏远地区:

D=branch-name; git branch -D $D; git push origin :$D

或者将下面的别名添加到您的~ /. git config ~ /. git config用法:git kill branch-name

[alias]
    kill = "!f(){ git branch -D \"$1\";  git push origin --delete \"$1\"; };f"

您也可以使用git remote prune origin

$ git remote prune origin
Pruning origin
URL: git@example.com/yourrepo.git
 * [pruned] origin/some-branchs

它利用和删除了远程跟踪分支和远程跟踪分支。git branch -r列表。

执行前执行前

git branch --delete <branch>

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

git ls-remote

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

之后我加了以下别名.gitconfig此选项允许我删除有分支或没有指定分支名称的分支。 如果没有通过参数, 分支名称默认为当前分支 。

[alias]
    branch-name = rev-parse --abbrev-ref HEAD     

    rm-remote-branch = !"f() { branch=${1-$(git branch-name)}; git push origin :$branch; }; f"
    rm-local-branch = !"f() { branch=${1-$(git branch-name)}; git checkout master; git branch -d $branch; }; f"
    rm-branch-fully = !"f() { branch=${1-$(git branch-name)}; git rm-local-branch $branch; git rm-remote-branch $branch; }; f"

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

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.