Skip to content

Instantly share code, notes, and snippets.

@anvk
Last active June 21, 2022 02:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anvk/cc835cd3844a70a410afcb7a6cdaf7ff to your computer and use it in GitHub Desktop.
Save anvk/cc835cd3844a70a410afcb7a6cdaf7ff to your computer and use it in GitHub Desktop.
Remote branch cleanup in Git
#!/bin/bash
# This Gist was inspired by the following posts:
# https://nickymeuleman.netlify.app/blog/delete-git-branches
# https://railsware.com/blog/git-housekeeping-tutorial-clean-up-outdated-branches-in-local-and-remote-repositories/
#
# This command helps find all stale non-merged branches on your remote that are more than X weeks old
#
# ARGUMENTS:
# Threshold in weeks for the stale branch
# Name of the author for the branch
#
# EXAMPLES:
#
# git_housekeeping_show_nonmerged
# This execution will find all non-merged branches that are more than 3 weeks old
#
# git_housekeeping_show_nonmerged 8
# This execution will find all non-merged branches that are more than 2 months old
#
# git_housekeeping_show_nonmerged 4 "Alexey Novak"
# This execution will find all non-merged branches that are more than 1 month old and belong to author Alexey Novak
#
alias git_housekeeping_show_nonmerged='function __git_housekeeping_show_nonmerged() {
echo Let me look for some stale branches for you... hold on
for branch in `git branch -r --no-merged | grep -v HEAD`; do
if [[ "$(git log $branch --since "${1:-3} weeks ago" -n 2 | wc -l)" -eq 0 ]]; then
l=`git show --format="%ci %cr \\t%an" $branch | head -n 1`
if [ ! -z "$2" ]
then
if [[ $l =~ "${2}" ]]
then
echo -e $l \\t$branch;
fi
else
echo -e $l \\t$branch;
fi
fi
done | sort -r
return 1
}; __git_housekeeping_show_nonmerged'
#
# This command helps find all stale already merged branches on your remote that are more than X weeks old
#
# ARGUMENTS:
# Threshold in weeks for the stale branch
# Name of the author for the branch
#
# EXAMPLES:
#
# git_housekeeping_show_nonmerged
# This execution will find all merged branches that are more than 3 weeks old
#
# git_housekeeping_show_nonmerged 8
# This execution will find all merged branches that are more than 2 months old
#
# git_housekeeping_show_nonmerged 4 "Alexey Novak"
# This execution will find all merged branches that are more than 1 month old and belong to author Alexey Novak
#
alias git_housekeeping_show_merged='function __git_housekeeping_show_merged() {
echo Let me look for some stale branches for you... hold on
for branch in `git branch -r --merged | grep -v HEAD`; do
if [[ "$(git log $branch --since "${1:-3} weeks ago" -n 2 | wc -l)" -eq 0 ]]; then
l=`git show --format="%ci %cr \\t%an" $branch | head -n 1`
if [ ! -z "$2" ]
then
if [[ $l =~ "${2}" ]]
then
echo -e $l \\t$branch;
fi
else
echo -e $l \\t$branch;
fi
fi
done | sort -r
return 1
}; __git_housekeeping_show_merged'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment