Skip to content

Instantly share code, notes, and snippets.

@phivh
Created October 12, 2020 16:15
Show Gist options
  • Save phivh/746c7764cc1fc375d3c2177c63cd3a03 to your computer and use it in GitHub Desktop.
Save phivh/746c7764cc1fc375d3c2177c63cd3a03 to your computer and use it in GitHub Desktop.
Delete all local branch that has not received any commits for a while
#!/bin/sh
date=$1
delete_local_branch () {
if [[ "$date" == "" ]]; then
echo "Missing date!"
exit
fi
for k in $(git branch | sed /\*/d); do
if [ -z "$(git log -1 --since='$date' -s $k)" ]; then
local_branch_name=$(echo $k | sed -e "s/origin\///")
echo deleting branch: $local_branch_name
git branch -d $local_branch_name
fi
done
}
delete_local_branch
@phivh
Copy link
Author

phivh commented Oct 13, 2020

Somehow, using this script directly

for k in $(git branch | sed /\*/d); do
  if [ -z "$(git log -1 --since='2020-07-30 00:00:00' -s $k)" ]; then
    local_branch_name=$(echo $k | sed -e "s/origin\///")
    echo deleting branch: $local_branch_name
# use -D instead if the branch didnt merge yet
    git branch -d $local_branch_name
    sleep 1
fi
done

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