Here's what I use in one of my build scripts to create a clean checkout of HEAD
(replace HEAD
with any branch or commit that you want, or use a shell variable):
if [ $(git status --porcelain -uall | wc -l) -gt 0 ]; then
echo "Creating pristine checkout of HEAD."
git reset --quiet --hard HEAD
git clean --quiet -fd
fi
The if
clause checks if there is anything to be done at all. You can leave it out if you do not care about the slight increase in efficiency it might (or might not even) bring.
If you want to also remove files that are in ignore rules (.gitignore
files), then you need to give the -x
option to git-clean
, and the --ignored
option to git-status
; you mentioned that you do not want that to happen.
I don't think any of the other calls that you make are necessary. Clearing the stash is not necessary (but it does not hurt, either).