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

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


当前回答

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

其他回答

我在我的.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
}

之后我加了以下别名.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 <branch-name>
git push origin :<branch-name>

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

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

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

就是这样。

使用 :

git push origin :bugfix  # Deletes remote branch
git branch -d bugfix     # Must delete local branch manually

如果您确定要删除,请运行

git branch -D bugfix

现在清理已删除的远程分支, 运行

git remote prune origin