是否有一种简单的方法可以删除所有远程对等分支不再存在的跟踪分支?
例子:
分支机构(本地和远程)
主人原始/主起源/bug-fix-a起源/bug-fix-b起源/bug-fix-c
在本地,我只有一个主分支。现在我需要处理bug-fix-a,所以我检查它,处理它,并将更改推到远程。接下来,我对bug-fix-b做同样的操作。
分支机构(本地和远程)
主人bug-fix-abug-fix-b型原始/主起源/bug-fix-a起源/bug-fix-b起源/bug-fix-c
现在我有本地分支机构master,bug-fix-a,bug--fix-b。主分支维护者将把我的更改合并到主分支中,并删除他已经合并的所有分支。
因此,当前状态为:
分支机构(本地和远程)
主人bug-fix-abug-fix-b型原始/主起源/bug-fix-c
现在我想调用一些命令来删除分支(在本例中为bug-fix-a、bug-fix-b),这些分支在远程存储库中不再表示。
它类似于现有命令git remote prune origin,但更类似于git local prune origin。
这将删除本地不存在的所有远程分支(在ruby中):
bs = `git branch`.split; bs2 = `git branch -r | grep origin`.split.reject { |b| bs.include?(b.split('/')[1..-1].join('/')) }; bs2.each { |b| puts `git push origin --delete #{b.split('/')[1..-1].join('/')}` }
解释:
# local branches
bs = `git branch`.split
# remote branches
bs2 = `git branch -r | grep origin`.split
# reject the branches that are present locally (removes origin/ in front)
bs2.reject! { |b| bs.include?(b.split('/')[1..-1].join('/')) }
# deletes the branches (removes origin/ in front)
bs2.each { |b| puts `git push origin --delete #{b.split('/')[1..-1].join('/')}` }
我已经用GitPython编写了一个Python脚本来删除远程不存在的本地分支。
import git
import subprocess
from git.exc import GitCommandError
import os
def delete_merged_branches():
current_dir = input("Enter repository directory:")
repo = git.Repo(current_dir)
git_command = git.Git(current_dir)
# fetch the remote with prune, this will delete the remote references in local.
for remote in repo.remotes:
remote.fetch(prune=True)
local_branches = [branch.name for branch in repo.branches]
deleted_branches = []
# deleted_branches are the branches which are deleted on remote but exists on local.
for branch in local_branches:
try:
remote_name = 'origin/'+ branch
repo.git.checkout(remote_name)
except GitCommandError:
# if the remote reference is not present, it means the branch is deleted on remote.
deleted_branches.append(branch)
for branch in deleted_branches:
print("Deleting branch:"+branch)
git_command.execute(["git", "branch", "-D",branch])
# clean up the work flow.
repo.git.checkout('master')
repo.git.pull()
if __name__ == '__main__':
delete_merged_branches()
希望有人觉得它有用,如果我错过了什么,请添加评论。
另一个答案,大量借鉴Patrick的答案(我喜欢这个答案,因为它似乎消除了任何关于去哪儿的歧义),将在git分支输出中匹配),但添加了*nix弯曲。
最简单的形式是:
git branch --list --format \
"%(if:equals=[gone])%(upstream:track)%(then)%(refname:short)%(end)" \
| xargs git branch -D
我的路径上有一个git-gone脚本:
#!/usr/bin/env bash
action() {
${DELETE} && xargs git branch -D || cat
}
get_gone() {
git branch --list --format \
"%(if:equals=[gone])%(upstream:track)%(then)%(refname:short)%(end)"
}
main() {
DELETE=false
while [ $# -gt 0 ] ; do
case "${1}" in
(-[dD] | --delete) DELETE=true ;;
esac
shift
done
get_gone | action
}
main "${@}"
注:--format选项似乎很新;我需要将git从2.10.something升级到2.16.3才能获得它。
编辑:修改为包含参考名的建议:来自Benjamin W。
NB2-我只在bash中进行了测试,因此有了hashbang,但可能可以移植到sh。