Home > Back-end >  How to remove all branches from a git repository based on a divergence threshold from master?
How to remove all branches from a git repository based on a divergence threshold from master?

Time:07-07

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 ?

CodePudding user response:

Use git branch -r to get all branches, then iterate for each to get the number (add a leading 0 for error)

git branch -r| while read branch; do
    nb=$(git rev-list --count main...$branch)
    if [ $nb -ge 1000 ]; then
       echo "$branch to delete"
    fi
done

just replace echo "$branch to delete" by git branch -D $branch

  •  Tags:  
  • git
  • Related