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

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


当前回答

我厌倦了勾引这个答案, 所以我采取了类似的方法 来回答克里格早些时候发布的答案。

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

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

每当我完成一个分支(例如, 合并为主人)时, 我在我的终端里运行以下功能:

gitdelete my-branch-name

...然后从源头和本地删除我的处名

其他回答

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

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"

如果您想要删除分支, 请先向要删除的分支以外的分支首选 。

git checkout other_than_branch_to_be_deleted

删除本地分支 :

git branch -D branch_to_be_deleted

删除远程分支 :

git push origin --delete branch_to_be_deleted

其它许多答案都会导致错误/ 警告。 这个方法比较愚蠢, 虽然您仍然需要 git 分支 - D 分支 - D 分支_ to_ deletete , 如果它没有完全合并到其它分支中, 比如 。

git checkout some_other_branch
git push origin :branch_to_delete
git branch -d branch_to_delete

如果您删除了远程分支, 不需要远程剪切。 它只用来获取您正在跟踪的仓库中可用的最新远程 。 我观察过 Git 抓取将会添加远程, 而不是移除它们 。 下面的例子说明 Git 远程 Pinene 何时会真正做点什么 :

用户 A 执行上述步骤。 用户 B 将运行以下命令以查看最新的远程分支 :

git fetch
git remote prune origin
git branch -r

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

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