删除远程分支的尝试失败 :
$ 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 origin :branch-or-tag-name
error: dst refspec branch-or-tag-name matches more than one.
error: failed to push some refs to 'git@github.com:SomeName/some-repo.git'
在此情况下, 您需要指定要删除分支, 而不是标记 :
git push origin :refs/heads/branch-or-tag-name
类似地, 要删除标签, 而不是您要使用的分支 :
git push origin :refs/tags/branch-or-tag-name
答案是好的, 但是, 如果您有一大堆分支, 逐个删除本地和远程的分支, 将会是一个无聊的任务 。 您可以使用此脚本将这些任务自动化 。
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 分支
如果您想要用一个单命令来完成这两个步骤, 您可以在您的~/.gitconfig
:
[alias]
rmbranch = "!f(){ git branch -d ${1} && git push origin --delete ${1}; };f"
或者,您可以从命令行使用
git config --global alias.rmbranch \
'!f(){ git branch -d ${1} && git push origin --delete ${1}; };f'
注 注 注 注 注注:如果使用-d
(lowercase d) , 分支分支只有在合并时才被删除 。 要强制删除, 您需要使用-D
(多例D)。
最初的几种方法对我行不通。
假设你有以下分支和远程分支,
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
就是这样。