Check if “git stash” stashed anything

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.

  • Start Tomcat on Console from Jenkins
  • How can I retrieve environment variables(Jenkins) in java
  • Ignore file in Subversion removes old values from svn:ignore property
  • How to run command in windows batch Command in jenkins
  • Splitting a multi-line environment variable into lines
  • Windows batch file to delete .svn files and folders
  • 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?

  • Multi-user Github Pull Requests
  • Get url of current path in Terminal SSH
  • git: list all files with owner/identity at first commit (or rather, the first user to commit the file)
  • git identify the most recent intersection of two branches
  • git status shows modifications even with autocrlf=false
  • Cannot build app when using gradle (Android Studio) and local maven repo
  • 4 Solutions collect form web for “Check if “git stash” stashed anything”

    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.

    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

    You can git diff and inspect the output. If there is none, then there is nothing to stash. Any issue with that strategy?

    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.

    Git Baby is a git and github fan, let's start git clone.