Cleaning up Git repos
Deleting old remote branches
If you have a repository that’s been around for long enough, you probably have a fair number of “stray branches”: deadends, false starts, orphans, etc. Let’s clean up all of the remote branches that haven’t been touched in the last 6 months:
1 2 3 4 5
for k in $(git branch -r | grep -E -v '>|master|prod'); do if [ -z "$(git log -1 --since='6 months ago' -s $k)" ]; then git push origin --delete $(cut -d"/" -f2- <<< "$k"); fi done
Walking through the various steps:
git branch -rlists all remote branches
grep -E -v '>|master|prod'filters that list of branches, removing any that have
origin/HEAD -> origin/master),
origin/prod) in their names.
- Iterating over each of these branches (as
git log -1 --since='6 months ago' -s $kchecks if the branch has been commited to in the last 6 months (you could obviously set the timeframe to whatever suited your needs).
cut -d"/" -f2- <<< "$k")trims a branch string like
origin/feature/some_branchto a string like
git push origin --delete $(...)removes that branch from the remote repository.
Deleting already-merged remote branches
You may also find yourself needing to remove remote branches that have also already been merged to
1 2 3 4
git branch -r --merged origin/master | grep -E -v '>|master|prod' | cut -d"/" -f2- | xargs git push origin --delete
Here we see many of the same basic sub-commands we used before. There is, however, one note-worthy difference. We specify the specific branch we want to check whether the remote branches have been merged into by specifying
origin/master. This ensures that only remote branches that have been merged into remote
master are passed to the next sub-command.
Deleting already-merged local branches
Finally, if you need to clean up your local repository, you can prune the local branches that have already been merged into
master in a similar way:
git branch -d $(git branch --merged master | grep -E -v '\*|master|prod')
A word of warning though: have local copies of these branches somewhere, just in case you delete a branch you want back at some point ;)