What is the purpose of git stash create and git stash store?

后端 未结 2 436
难免孤独
难免孤独 2021-02-05 09:02

From the documentation at git-scm, there are two git stash commands that mention relevance to scripting, but not general use:

create

2条回答
  •  旧时难觅i
    2021-02-05 09:52

    Unfortunately the nice example that Andrew showed above does not work in all cases, because:

    • If there are local changes then git stash create will create an unreferenced commit, but it won't actually clear the local changes.

    • If there are not any local changes, then it won't create a commit at all (as BlackVegetable pointed out). In that case we should not apply at the end.

    • (And minor: Andrew forgot to keep and use the commit ID produced by create.)

    With that in mind, it appears to me that the usage should be like this:

    # Save the local changes, keep a reference to them, and clear them
    stashed_commit="$(git stash create)"
    git reset --hard
    
    # Do your thing
    git fetch
    git rebase
    
    # If there were local changes, then restore them
    if [ -n "${stashed_commit}" ]
    then git stash apply "${stashed_commit}"
    fi
    

    Unwieldy to say the least!

    Alas. It would be far simpler if I could just git stash save --allow-empty at the top, and git stash pop at the bottom.

    I would love to be wrong. Please correct me!

提交回复
热议问题