删除远程分支的尝试失败 :
$ 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
本地和远程分支?
这是所有其它答案的混结。 它需要Ruby 1.9. 3+, 并经过测试仅仅OS X上写着
调用此文件git-remove
,然后使用,例如,git remove temp
.
#!/usr/bin/env ruby
require 'io/console'
if __FILE__ == $0
branch_name = ARGV[0] if (ARGV[0])
print "Press Y to force delete local and remote branch #{branch_name}..."
response = STDIN.getch
if ['Y', 'y', 'yes'].include?(response)
puts "\nContinuing."
`git branch -D #{branch_name}`
`git branch -D -r origin/#{branch_name}`
`git push origin --delete #{branch_name}`
else
puts "\nQuitting."
end
end
答案是好的, 但是, 如果您有一大堆分支, 逐个删除本地和远程的分支, 将会是一个无聊的任务 。 您可以使用此脚本将这些任务自动化 。
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_to_delete
如果未完全合并到some_other_branch
例如。
git checkout some_other_branch
git push origin :branch_to_delete
git branch -d branch_to_delete
如果您删除了远程分支, 不需要远程剪切。 它只用来获取您正在跟踪的仓库中可用的最新的远程 。 我观察到了git fetch
添加遥控器, 而不是删除它们。 这里举例说明何时git remote prune origin
将实际做一些事情:
用户 A 执行上述步骤。 用户 B 将运行以下命令以查看最新的远程分支 :
git fetch
git remote prune origin
git branch -r