Skip to content

Instantly share code, notes, and snippets.

@yi-jiayu
Created October 1, 2019 12:49
Show Gist options
  • Save yi-jiayu/737297bc4cf340eb1811edbc96e08196 to your computer and use it in GitHub Desktop.
Save yi-jiayu/737297bc4cf340eb1811edbc96e08196 to your computer and use it in GitHub Desktop.
Script to check if a local Git repository can be deleted safely without losing any work
#!/usr/bin/env bash
# Check if the working tree is clean.
# The working tree is clean if the output of `git status --porcelain` is empty.
[ -z "$(git status --porcelain)" ] || echo "Working tree not clean."
# Check if there are any stashes.
# There are no stashes if the output of `git stash list` is empty.
[ -z "$(git stash list)" ] || echo "Stash list not empty."
# Check if any local branch does is not tracking a remote branch or is ahead of its upstream branch.
git for-each-ref --shell \
--format='case %(upstream:trackshort) in
"" )
echo "Branch %(refname:short) has no upstream." ;;
">" )
echo "Branch %(refname:short) is ahead of upstream branch %(upstream:short)." ;;
esac' \
refs/heads/ | . /dev/stdin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment