问题
I've been advised to avoid git pull --autostash, and instead use:
git alias.pull-autostash '!git stash push && git pull --rebase && git stash pop'
When there are no changes in the index or working tree, doing:
$ git stash push
gives:
No local changes to save
An additional problem is that the exit status is 0
.
Any stash pop
would then pop
something which wasn't pushed.
How do I force an empty stash to be created, like git commit --allow-empty
?
回答1:
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:
- Both fail: there was no stash before and is no stash after.
- The first fails, the second succeeds: there was no stash before, and there is now, hence one was created.
- Both succeed, with the two output strings matching (no stash created) or differing (stash created).
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.
回答2:
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)'
来源:https://stackoverflow.com/questions/52568548/git-stash-exits-0-but-no-stash-created