Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 951
Hand-E-Food Avatar asked Jul 01 '14 23:07

Hand-E-Food


People also ask

Where are git stashes stored?

All are stored in . git/refs/stash . git stash saves stashes indefinitely, and all of them are listed by git stash list . Please note that dropping or clearing the stash will remove it from the stash list, but you might still have unpruned nodes with the right data lying around.

What is the command to see all the saved stashes in a repository?

Git Stash List (Check the Stored Stashes) To check the stored stashes, run the below command: Syntax: $ git stash list.

Does git stash save untracked files?

In order to stash untracked files, add the “–include-untracked” option to your “git stash” initial command. Alternatively, you can simply use the “-u” which is equivalent to the untracked longer version.


2 Answers

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.

like image 153
Strikeskids Avatar answered Sep 30 '22 13:09

Strikeskids


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 
like image 25
Neil Neyman Avatar answered Sep 30 '22 13:09

Neil Neyman