Skip to content

Instantly share code, notes, and snippets.

@nottrobin
Created April 28, 2018 18:53
Show Gist options
  • Select an option

  • Save nottrobin/b1b0e99da13ae71c6c2f1dc118fdb9a0 to your computer and use it in GitHub Desktop.

Select an option

Save nottrobin/b1b0e99da13ae71c6c2f1dc118fdb9a0 to your computer and use it in GitHub Desktop.
Clean and possibly delete a Git repo if it's completely synced with the remotes
set -e
rm -rf env/ env2/ env3/
if [ ! -d .git ]; then
echo ""
echo "==="
echo "Not a git repository: `pwd`"
echo "==="
echo ""
exit
fi
if [ $(git s | wc -l) -gt 1 ]; then
echo ""
echo "========="
echo "!! Repository `pwd` has uncommitted changes"
echo "========="
git s
echo ""
exit
fi
echo "# Switching to master branch"
git checkout master
echo "# Pulling latest changes and deleting merged branches"
git update > /dev/null || true
echo "# Removing branches with missing remotes"
git fetch -p && git branch -vv | sed 's/^* / /' | awk '/: gone]/{print $1}' | xargs git branch -D || true
number_unpushed_branches=$(git log --branches --not --remotes --no-walk --decorate --oneline | wc -l)
if [ $number_unpushed_branches -gt 0 ]; then
echo ""
echo "========="
echo "!! Unpushed branches on `pwd`:"
echo "========="
git log --branches --not --remotes --no-walk --decorate --oneline
echo ""
exit
fi
echo ""
echo "========="
echo "+ We're all good on `pwd`. Deleting."
echo "========="
rm -rf `pwd`
echo "Deleted `pwd`"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment