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

倖福魔咒の 提交于 2019-12-03 04:57:16

You can use git stash create when you're writing scripts that need to stash as an implementation detail and you don't want to disturb the user's stash reflog.

Depending on what happens next, you might (in the case of error, say) decide you do want to disturb the stash reflog after all, at which point you can use git stash store.

Obviously a regular stash can be implemented in terms of create then store, but I can also imagine it being used in a hypothetical update-branch command that does something like this:

git stash create
git fetch
git rebase
git stash apply

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!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!