git stash exits 0 but no stash created

前端 未结 2 1177
太阳男子
太阳男子 2021-01-22 07:59

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         


        
相关标签:
2条回答
  • 2021-01-22 08:41

    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.

    0 讨论(0)
  • 2021-01-22 08:44

    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)'
    
    0 讨论(0)
提交回复
热议问题