Skip to content

Instantly share code, notes, and snippets.

@borisdiakur
Forked from antonio/delete_rebased_and_merged_branches.sh
Last active September 28, 2023 07:39
Show Gist options
  • Save borisdiakur/61837a76a5302309069a558e1f8eff0a to your computer and use it in GitHub Desktop.
Save borisdiakur/61837a76a5302309069a558e1f8eff0a to your computer and use it in GitHub Desktop.
Deletes branches which were rebased and merged into main.
#!/bin/sh
git stash
git checkout main
git fetch
git pull --rebase
for branch in $(git branch -a | sed 's/^\s*//' | sed 's/^remotes\///' | grep -v 'main$'); do
if [[ ! "$branch" =~ "origin/" ]]; then
last_commit_msg="$(git log --oneline --format=%f -1 $branch)"
if [[ "$(git log --oneline --format=%f | grep $last_commit_msg | wc -l)" -eq 1 ]]; then
git branch -D $branch
fi
fi
done
{
"name": "npx-git-tidy",
"description": "Deletes branches which were rebased and merged into main.",
"version": "1.0.0",
"bin": "./git-tidy.sh"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment