Skip to content

Instantly share code, notes, and snippets.

@adurbalo
Last active October 24, 2022 18:44
Show Gist options
  • Save adurbalo/b09225c3055ae3bbe5e99fa29419a81d to your computer and use it in GitHub Desktop.
Save adurbalo/b09225c3055ae3bbe5e99fa29419a81d to your computer and use it in GitHub Desktop.
Clean up old branches in repo
echo `git fetch --prune origin`
isRemoveEnabled=false
while [ -n "$1" ]
do
case "$1" in
--remove)
isRemoveEnabled=true
;;
-) shift
break
;;
*) echo "$1 unknown parameter, available options is [--remove]"
exit 1
;;
esac
shift
done
for branch in $(git branch -r | sed /\*/d); do
if [ -z "$(git log -1 --since='90 days ago' -s $branch)" ]; then
if [[ $branch != origin* ]]; then
echo "⏭ Skipping non origin"
continue
fi
if [[ $branch == origin/release/* ]]; then
echo "⏭ Skipping: $branch"
continue
fi
if [[ $branch == origin/maste* ]]; then
echo "⏭ Skipping: $branch"
continue
fi
if [[ $branch == origin/main* ]]; then
echo "⏭ Skipping: $branch"
continue
fi
if [[ $branch == origin/HEAD* ]]; then
echo "⏭ Skipping: $branch"
continue
fi
if [[ $branch == origin/develop* ]]; then
echo "⏭ Skipping: $branch"
continue
fi
echo `git show --format="%ci %cr %an" $branch | head -n 1` \\t$branch
if $isRemoveEnabled ; then
prefix="origin/"
string="$branch"
branchName=${string/#$prefix}
echo "🗑 Removing..."
git push --delete origin $branchName
fi
fi
done
@adurbalo
Copy link
Author

adurbalo commented Jul 6, 2022

./repo_clean.sh in repo root dir to print list of old branches
./repo_clean.sh --remove to delete branches

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment