Skip to content

Instantly share code, notes, and snippets.

@npasserini
Created March 23, 2017 11:38
Show Gist options
  • Select an option

  • Save npasserini/7eecdc120581f9d66b5da71b62a38f12 to your computer and use it in GitHub Desktop.

Select an option

Save npasserini/7eecdc120581f9d66b5da71b62a38f12 to your computer and use it in GitHub Desktop.
Removes already pushed branches from your local repository.
currentBranch=$(git rev-parse --abbrev-ref HEAD)
ignore=(dev master)
function red {
echo -e "\e[1;31m$1\e[0m"
}
function green {
echo -e "\e[1;32m$1\e[0m"
}
function yellow {
echo -e "\e[1;33m$1\e[0m"
}
function ignored {
for branch in ${ignore[@]}; do
if [ $1 = $branch ]; then return 0; fi
done
return 1
}
for branch in $(git for-each-ref --format='%(refname:short)' refs/heads); do
if $(ignored $branch); then continue; fi
if [ $branch = $currentBranch ]; then
echo Ignoring current branch $(green $branch)
continue
fi
upstream=$(git rev-parse --symbolic-full-name $branch@{u} 2>/dev/null)
if [ $upstream ]; then
pendingCommits=$(git rev-list --count $upstream..$branch)
if [ $pendingCommits -eq 0 ]; then
git branch -d $branch &> /dev/null && echo Deleted branch $branch
else
echo Ignoring branch $(yellow $branch) as it has $pendingCommits not-pushed commits
fi
else
echo Ignoring branch $(red $branch) because it has no upstream defined
fi
done
@npasserini

Copy link
Copy Markdown
Author

Please note that this anayses "pushed" instead of "merged" branches. This difference is useful when using a "squash and merge" strategy for your PRs, because your branches never get really "merged".

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