[英]How to remove all branches from a git repository based on a divergence threshold from master?
On my project I need to clean up a repository that has hundreds of old branches that are no longer relevant.在我的项目中,我需要清理一个包含数百个不再相关的旧分支的存储库。
I would like to remove the branches that have more than 1000 revisions away from master.我想从 master 中删除超过 1000 个修订的分支。
I found the command to find out the number of divergence of a branch:我找到了找出分支分歧数量的命令:
git rev-list --count master...release/2.49.0
output: 1299 output: 1299
I don't see how to use the git for-each-ref
command.我看不到如何使用
git for-each-ref
命令。
Is it possible to have a command that parse branch, count divergence and if the threshold is reach, perform a delete?是否有可能有一个命令来解析分支,计算分歧,如果达到阈值,执行删除?
Use git for-each-ref
to get all branches, then iterate for each to get the number (add a leading 0 for error)使用
git for-each-ref
获取所有分支,然后对每个分支进行迭代以获取数字(添加前导 0 表示错误)
git for-each-ref refs/remotes --format="%(refname)"| sed 's!refs/remotes/!!g'| while read branch; do
nb=$(git rev-list --count main...$branch)
if [ $nb -ge 5000 ]; then
echo "$branch to delete"
fi
done
just replace echo "$branch to delete"
by git branch -D $branch
只需将
echo "$branch to delete"
替换为git branch -D $branch
Thanks for all Ôrel.感谢所有Ôrel。
Finally here is the code I used on the repo:最后,这是我在 repo 中使用的代码:
git branch -r --format='%(refname:short)' | while read branch; do
fromBranch=develop
nb=$(git rev-list --count $fromBranch...$branch)
subject="$branch"
prefix="origin/"
branchName=${subject#"$prefix"}
if [ $nb -ge 1000 ]; then
echo "$branchName diverged from $fromBranch by $nb revisions"
# Uncomment to delete branch on remote
# git push origin --delete $branchName
fi
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.