gitgitpython

Git stash push with no modification


Usually when I run:

git stash push --all

if I don't have any modification, nothing is pushed on the stash. Would it be possible to force a push even in this case, so that git stash push --all && git stash pop always gives the identity?

I'm asking because in some script I want to stash push, do some stuff, and stash pop, and if the stash push don't create a new stash, stash pop may pop older and unrelated stash. Another solution would be to detect from GitPython that no stash happened (I would say by comparing the output of two consecutives repo.git.stash("list")), but it would also complicates a bit the code.

Thank you!


Solution

  • Instead of git stash push, use git stash create. This returns a SHA if successful, otherwise it returns nothing.

    Then;

    So you could do something like:

    SHA=$(git stash create)
    if [[ -n "$SHA" ]]; then
      git stash store "$SHA"
    fi
    # ... DoTheThing();
    if [[ -n "$SHA" ]]; then
      git stash pop
    fi