I'm using git stash push -a in a script to save uncommitted changes and then sometime later run git stash pop in the same script to restore the stashed changes. I've run into a problem in which git stash push -a occasionally reports "No local changes to save" and no no stash is created. This causes the subsequent git stash pop to fail with exit code 1. Is there a way I can detect when git stash push -a reports "No local changes to save" so that I know that no stash is created (short of checking the git stash push -a output) and thus not perform the git stash pop?
2 Answers
Botje's "cheeky suggestion" of creating a temporary file so that git stash push -a has at least one file to stash worked for me. Just remember to delete the temporary file after running git stash pop.
Comments
I would rather suggest checking in those changes as normal, but temporary commits and stop using stash because normal commits are much safer in they do not have dangerous, complicated quirks like stash have.
For your use case, you can save with
git commit -am "AUTOSAVE myscript.sh "$(date +%y-%m-%d_%H-%M-%S)""
and then unsave with
if [[ "$(git log --format=format:%s HEAD^..HEAD)" == "AUTOSAVE myscript.sh"* ]]
then
git reset HEAD^
fi
2 Comments
Geoff Alexander
I don't see how
git reset HEAD^ restores the uncommitted changes; rather, I would think that it deletes them.Geoff Alexander
Also, I don't think
git commit -a commits untracked files (i.e. new files you have not told Git about).
touch $(uuidgen)before stashing :)git stashand test if it is equal toNo local changes to save. In Bash for example,output=$(git stash push -a);if [[ "$output" = "No local changes to save" ]];then no-git-stash-pop;fi.git stashexamine the very changes. Likegit update-index -q --refresh && git diff-index --quiet HEAD. Orgit ls-files --deleted --modified --others --unmerged --killed --exclude-standard --directory --no-empty-directory