Created
October 1, 2019 12:49
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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