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

$ 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本地和远程分支?


当前回答

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

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

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

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

gitdelete my-branch-name

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

其他回答

另一种办法是:

git push --prune origin

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

git push --mirror

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

我在我的.bash_alians 文件中创建了以下方便功能 :

git-delete-branch() 
{ 
    if [[ -n $1 ]]; then
        git checkout master > /dev/null;
        branch_name="$1";
        echo "Deleting local $branch_name branch...";
        git branch -D "$branch_name";
        echo "Deleting remote $branch_name branch...";
        git push origin --delete "$branch_name";
        git remote prune origin;
        echo "Your current branches are:";
        git branch -a;
    else
        echo "Usage: git-delete-branch <branch_name>";
    fi
}

最初的几种方法对我行不通。

假设你有以下分支和远程分支,

Local : Test_Branch
Remote: remotes/origin/feature/Test_FE

正确设置上方为您本地分支以跟踪您想要删除的远程分支 。

git branch --set-upstream-to=remotes/origin/feature/Test_FE Test_Branch

然后删除远程分支执行此任务

git push origin --delete Test_Branch

然后删除本地分支,按照命令执行

git branch -D Test_Branch

就是这样。

删除远程分支的命令行的替代选项是GitHub 分支页面.

例如,见:https://github.com/angular/angular.js/branches

发现于Code -> BranchesGitHub 库页面。

我一般都更喜欢指挥线 但这个GitHub 页面显示更多信息,树的枝枝的枝枝,例如最新更新日期和用户, 和未来和后承诺数在与大量分支机构打交道时非常有用。

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

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.