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

$ 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分支?


当前回答

AcolAJ86 和 apentwarr 的回答都非常相似。 我反复地在两个小组之间走来走去,试图了解支持子模块替换的更好方法。 下面是两者的组合。

首先将 Git Bash 导航到 Git 仓库的根部, 以便分割 。 我这里的例子就是 ~/ Documents/ irstRepo( master) 。

# Move the folder at prefix to a new branch
git subtree split --prefix=SubFolderName/FolderToBeNewRepo --branch=to-be-new-repo

# Create a new repository out of the newly made branch
mkdir ~/Documents/NewRepo
pushd ~/Documents/NewRepo
git init
git pull ~/Documents/OriginalRepo to-be-new-repo

# Upload the new repository to a place that should be referenced for submodules
git remote add origin git@github.com:myUsername/newRepo.git
git push -u origin master
popd

# Replace the folder with a submodule
git rm -rf ./SubFolderName/FolderToBeNewRepo
git submodule add git@github.com:myUsername/newRepo.git SubFolderName/FolderToBeNewRepo
git branch --delete --force to-be-new-repo

下面是上面的复制件,上面有自定义的可定义名称替换和使用 HTTPS 。 根文件夹现在为 ~/ Documents/_ Shawn/ UnityProjects/ SoProject( master) 。

# Move the folder at prefix to a new branch
git subtree split --prefix=Assets/SoArchitecture --branch=so-package

# Create a new repository out of the newly made branch
mkdir ~/Documents/_Shawn/UnityProjects/SoArchitecture
pushd ~/Documents/_Shawn/UnityProjects/SoArchitecture
git init
git pull ~/Documents/_Shawn/UnityProjects/SoProject so-package

# Upload the new repository to a place that should be referenced for submodules
git remote add origin https://github.com/Feddas/SoArchitecture.git
git push -u origin master
popd

# Replace the folder with a submodule
git rm -rf ./Assets/SoArchitecture
git submodule add https://github.com/Feddas/SoArchitecture.git
git branch --delete --force so-package

其他回答

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

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

您也可以使用 Git 远程光素来源来做此操作

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

它从 Git 分支 - r 列表中提取并删除远程跟踪分支 。

答案是好的, 但是, 如果您有一大堆分支, 逐个删除本地和远程的分支, 将会是一个无聊的任务 。 您可以使用此脚本将这些任务自动化 。

branch_not_delete=( "master" "develop" "our-branch-1" "our-branch-2")

for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do

    # Delete prefix remotes/origin/ from branch name
    branch_name="$(awk '{gsub("remotes/origin/", "");print}' <<< $branch)"

    if ! [[ " ${branch_not_delete[*]} " == *" $branch_name "* ]]; then
        # Delete branch remotly and locally
        git push origin :$branch_name
    fi
done

列出您不想删除的在遥控器分支上的循环的分支, 如果它们不在我们的“ 保存列表” 中, 则将其删除 。

来源: 立即删除 Git 分支

本地删除 :

要删除本地分支,请使用:

git branch -d <branch_name>

强行删除分支,使用 -D 而不是 -d 。

git branch -D <branch_name>

远程删除 :

有两个选项:

git push origin :branchname

git push origin --delete branchname

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

一班级命令,删除本地和远程两个部分:

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

或添加以下别名到 ~/. gitconfig 的 ~ /. gitconfig 。 用法: git 杀死分支名称

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