52

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?

Hand-E-Food
  • 12,368
  • 8
  • 45
  • 80
  • 2
    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. – Penz Dec 22 '18 at 07:59
  • 1
    Editing this to be more bash like conflicts with it being a Windows Command script, and actaully makes it invalid. ;-) – Hand-E-Food Sep 18 '19 at 21:22

4 Answers4

50

What you want is:

git stash list

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.

jumping_monkey
  • 5,941
  • 2
  • 43
  • 58
Strikeskids
  • 3,932
  • 13
  • 27
  • 1
    I like this answer. It's robust – New Alexandria May 27 '16 at 15:44
  • 4
    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` – david.tanner Apr 21 '21 at 19:31
  • @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 – Shobhit Puri Sep 09 '21 at 20:55
15
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
Neil Neyman
  • 2,116
  • 16
  • 21
  • 2
    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. – Hand-E-Food Jul 01 '14 at 23:57
  • 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) – Neil Neyman Jul 02 '14 at 00:07
13

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.

franso
  • 131
  • 1
  • 4
1

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?

New Alexandria
  • 6,951
  • 4
  • 57
  • 77
  • Unless you provide specific options to `git stash`, for me it's working. – Jean Paul Mar 23 '21 at 10:22
  • 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 Apr 21 '21 at 19:28