I\'ve been advised to avoid git pull --autostash, and instead use:
git alias.pull-autostash \'!git stash push && git pull --rebase && git stash p
For scripting, use git stash create
(which produces the hash ID of the created stash on standard output, or nothing on standard output if no stash was created). You can then use git stash store
to insert the created stash as stash@{0}
, if and only if one was actually created.
If your Git is too old to have git stash create
, you can run two git rev-parse
commands on refs/stash
before and after running git stash save
.1 These can:
Thus, if you use --quiet --verify
and capture the actual output from each, you can tell whether a stash was created. That is, the programming fragment that applies here is:
old=$(git rev-parse --quiet --verify refs/stash)
git stash save || die ...
new=$(git rev-parse --quiet --verify refs/stash)
if [ "$old" != "$new" ]; then
made_stash=true
else
made_stash=false
fi
... do things ...
if $made_stash; then ... do things with the stash you made ...
(I recommend avoiding both git stash
and git pull
except in very limited, specialized circumstances. I've had too many bad experiences with them.)
1If your Git lacks git stash create
, it probably predates git stash push
as well and hence you need git stash save
instead.
This answer does not answer how you stash an empty change but it should solve your problem.
Check if there are pending changes first (see https://stackoverflow.com/a/3879077/3526980)
$ git alias.pull-autostash '(git diff-index --quiet HEAD -- && git pull --rebase) || (git stash push && git pull --rebase && git stash pop)'