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

$ 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

其他回答

之后我加了以下别名.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 contact-form

要删除远程分支 :

git push origin --delete contact-form

您也可以使用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 -d <branch-name>
git push origin :<branch-name>

另一种办法是:

git push --prune origin

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

git push --mirror

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