[英]How to remove all branches from a git repository based on a divergence threshold from master?
在我的項目中,我需要清理一個包含數百個不再相關的舊分支的存儲庫。
我想從 master 中刪除超過 1000 個修訂的分支。
我找到了找出分支分歧數量的命令:
git rev-list --count master...release/2.49.0
output: 1299
我看不到如何使用git for-each-ref
命令。
是否有可能有一個命令來解析分支,計算分歧,如果達到閾值,執行刪除?
使用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
只需將echo "$branch to delete"
替換為git branch -D $branch
感謝所有Ôrel。
最后,這是我在 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.