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.
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
I don't see how to use the git for-each-ref
command.
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 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
Thanks for all Ôrel.
Finally here is the code I used on the 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.