Git Repository Clean Up

Irfan Baig
1 min readMay 11, 2022
Git Cleanup

If you have a git repository with a lot of branches and unused or dummy tags which you want to get rid of. Here is the list of commands that can help you.

First checkout the develop / master / main branch which is your default branch for merging all your PRs.

List Merged Branches

git branch -r --merged

List Merged Branches except for some branches

git branch -r --merged | egrep -v "(^\*|master|develop|main)" 

Delete merged branches and push to origin

git branch -r --merged | egrep -v "(^\*|master|develop)" | sed 's/origin\///' | xargs -n 1 git push origin --delete

Delete Tags

List tags

git tag -l "beta.*"

Delete tags local

git tag -d $(git tag -l "beta.*")

Delete tags remote

git tag -l "beta.*" | xargs -n 1 git push --delete origin

your teammates can run git fetch — prune to get the updates.

Delete Releases — Draft / Pre-release

Install

brew install gh

Delete

gh release list --limit 9999 | awk -F '\t' '$2 == "Draft" {print $3}' | xargs -L1 gh release delete --yes

Thank you for passing by :)

--

--

Irfan Baig

Engineering Manager #java #nodejs #PHP #backend #developer ibaig.com