Check if "git stash" stashed anything

17,324

Solution 1

git stash allows you to provide a message. You can use a generated token as your message so that you know it won't conflict with other git stash messages.

Then, when you want to check whether or not to pop, simply check if the git stash list output contains your token. If so, pop the stash.

Solution 2

git stash list #get a listing of all stashes to parse
git stash show -p stash@{0} #replace 0 with number of relevant stash from list

Solution 3

On Linux, you can use git stash list | wc -l to count the number of stash entries. If there was nothing to stash, then this returns the same before and after your actual git stash. I'm not sure if you can use this on Windows though.

Solution 4

You can git diff and inspect the output.

  • If there is none, then there is nothing to stash.
  • If there is output, then you can grep to parse it for specific things you need.

Any issue with that strategy?

Share:
17,324

Related videos on Youtube

Hand-E-Food
Author by

Hand-E-Food

Updated on June 09, 2022

Comments

  • Hand-E-Food
    Hand-E-Food almost 2 years

    I have a Windows Command script designed to merge the dev branch into a project branch. It starts by reading the current branch name, stashing changes, fetching and merging the dev and project branches, then switches back to the original branch and pops the stash.

    The issue is there might not be any changes to stash. This leaves the previous stash at the top of the stack. When it gets to the end of the script and pops the stash, it's popping the previous stash which is unrelated to the current branch.

    Set SourceBranch=dev
    Set ProjectBranch=project
    
    :: Stash current changes.
    For /F "tokens=1,2" %%a In ('Git branch -q') Do If "%%a"=="*" Set CurrentBranch=%%b
    Git stash save -u
    
    :: Pull latest source branch.
    Git checkout %SourceBranch%
    Git pull
    For /F "tokens=1,3" %%a In ('Git branch -q -v') Do If "%%a"=="*" Set MergeHash=%%b
    
    :: Merge source into project branch.
    Git checkout %ProjectBranch%
    Git pull
    Git merge --commit %MergeHash%||Exit 1
    
    :: Return to original branch.
    Git checkout %CurrentBranch%
    Git stash pop
    

    How can I get feedback from Git stash or Git status to determine whether I need to pop the stash?

    • Penz
      Penz over 5 years
      It's woth noting that git pull and git rebase now have a --autostash argument, with an corresponding rebase.autoStash configuration. It was added in version 2.6.
    • Hand-E-Food
      Hand-E-Food over 4 years
      Editing this to be more bash like conflicts with it being a Windows Command script, and actaully makes it invalid. ;-)
  • Hand-E-Food
    Hand-E-Food almost 10 years
    How does that help me determine whether anything was stashed? If there are no changes, nothing is stashed and any number of stashes might exist previously.
  • annelie
    annelie almost 10 years
    Grep the output of git stash list for the branch name you are interested in. Although that assumes you dont have other stashes n the same branch (you seemed to indicate you did not)
  • New Alexandria
    New Alexandria almost 8 years
    I like this answer. It's robust
  • Ciro Santilli OurBigBook.com
    Ciro Santilli OurBigBook.com over 3 years
    I can't believe it, but I'm going to do this.
  • geek-merlin
    geek-merlin about 3 years
    This looks like the most robust to me.
  • Jean Paul
    Jean Paul about 3 years
    Unless you provide specific options to git stash, for me it's working.
  • david.tanner
    david.tanner about 3 years
    Using this strategy, in my case, you would need to do an if twice to see first if you should call stash, and then second if you are going to call pop.
  • david.tanner
    david.tanner about 3 years
    I like this approach the best. Here are the three lines I ended up using. GIT_STASH_MESSAGE="${RANDOM}" git stash -m "${GIT_STASH_MESSAGE}" git stash list | grep "${GIT_STASH_MESSAGE}" && git stash pop --index
  • Shobhit Puri
    Shobhit Puri over 2 years
    @david.tanner Is it git stash push -m "${GIT_STASH_MESSAGE}" The command you mentioned to stash with a message didn't work for me on Mac. However add 'push' worked