How can you tell if a git stash is no longer required?

后端 未结 3 1406
故里飘歌
故里飘歌 2021-02-20 10:55

Is it possible to tell whether a stash has already been applied, and therefore is no longer required, without doing git stash apply? Assume that I\'m only using one

相关标签:
3条回答
  • 2021-02-20 11:23

    Just make a diff and you will see.

    git diff HEAD stash@{0}

    0 讨论(0)
  • 2021-02-20 11:31

    You can use the following shell script to get git stash list prefixed with checkmarks if they have already been applied or there is no need to apply them as there is no difference.

    git stash list | while read line; do \
      ref=${line%%:*}; \
      prefix=$(test $(git diff $ref | wc -l) = "0" && echo "✔  " || echo "   "); \
      echo "$prefix$line"; \
    done
    

    This will give you a list like:

    ✔  stash@{0}: WIP on develop: 77a1a66 send 'social.share' message via 'view-req-relay'...
       stash@{1}: WIP on bigcouch: 4bfa3af added couchdb filters...
    

    And if you like it you can add it as a git alias like that:

    git config --global --add alias.stash-list '!git stash list | while read line; do   ref=${line%%:*};   prefix=$(test $(git diff $ref | wc -l) = "0" && echo "✔  " || echo "   ");   echo "$prefix$line"; done'
    git stash-list
    

    (tested with bash and zsh)

    0 讨论(0)
  • 2021-02-20 11:43

    If you use git diff stash, make sure to use Git 2.30 (Q1 2021).

    Before that, the code to see if "git stash drop"(man) can safely remove refs/stash has been made more careful.

    See commit 4f44c56 (24 Oct 2020) by René Scharfe (rscharfe).
    (Merged by Junio C Hamano -- gitster -- in commit 30f5257, 18 Nov 2020)

    stash: simplify reflog emptiness check

    Reported-by: Marek Mrva
    Signed-off-by: René Scharfe

    Calling rev-parse to check if the drop subcommand removed the last stash and treating its failure as confirmation is fragile, as the command can fail for other reasons, e.g. because the system is out of memory.

    Directly check if the reflog is empty instead, which is more robust.

    So the use of git rev-parse --verify --quiet to check if the stash dropped was the ast one has been replaced.

    0 讨论(0)
提交回复
热议问题