[英]Check if “git stash” stashed anything
I have a Windows Command script designed to merge the dev branch into a project branch. 我有一个Windows Command脚本,旨在将dev分支合并到项目分支中。 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? 如何从
Git stash
或Git status
获取反馈以确定是否需要弹出存储?
git stash
allows you to provide a message. git stash
允许您提供消息。 You can use a generated token as your message so that you know it won't conflict with other git stash messages. 您可以使用生成的令牌作为消息,以便您知道它不会与其他git存储消息冲突。
Then, when you want to check whether or not to pop, simply check if the git stash list
output contains your token. 然后,当您想要检查是否要弹出时,只需检查
git stash list
输出是否包含您的令牌。 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. 你可以
git diff
并检查输出。 If there is none, then there is nothing to stash
. 如果没有,那么就没有什么可
stash
。 Any issue with that strategy? 该策略有任何问题吗?
On Linux, you can use git stash list | wc -l
在Linux上,您可以使用
git stash list | wc -l
git stash list | wc -l
to count the number of stash entries. git stash list | wc -l
计算存储条目的数量。 If there was nothing to stash, then this returns the same before and after your actual git stash
. 如果没有任何东西要存储,那么这会在你的实际
git stash
之前和之后返回相同的内容。 I'm not sure if you can use this on Windows though. 我不确定你是否可以在Windows上使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.