检查“git stash”是否藏有任何东西

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24520791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:09:45  来源:igfitidea点击:

Check if "git stash" stashed anything

gitbatch-filegit-stash

提问by Hand-E-Food

I have a Windows Command script designed to merge the devbranch 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.

我有一个 Windows 命令脚本,旨在将dev分支合并到一个项目分支中。它首先读取当前分支名称,存储更改,获取并合并 dev 和 project 分支,然后切换回原始分支并弹出存储。

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.

问题是 stash 可能没有任何变化。这会将先前的存储留在堆栈的顶部。当它到达脚本的末尾并弹出存储时,它会弹出与当前分支无关的前一个存储。

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 stashor Git statusto determine whether I need to pop the stash?

我如何从Git stashGit status确定我是否需要弹出藏匿处获得反馈?

采纳答案by Strikeskids

git stashallows 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.

git stash允许您提供消息。您可以使用生成的令牌作为您的消息,以便您知道它不会与其他 git stash 消息冲突。

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

然后,当您想检查是否弹出时,只需检查git stash list输出是否包含您的令牌。如果是这样,弹出藏匿处。

回答by Neil Neyman

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

回答by New Alexandria

You can git diffand inspect the output.

您可以git diff检查输出。

  • If there is none, then there is nothing to stash.
  • If there is output, then you can grepto parse it for specific things you need.
  • 如果没有,那么就没有什么可做的stash
  • 如果有输出,那么您可以grep针对您需要的特定内容对其进行解析。

Any issue with that strategy?

那个策略有问题吗?

回答by franso

On Linux, you can use git stash list | wc -lto 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.

在 Linux 上,您可以使用git stash list | wc -l来计算 stash 条目的数量。如果没有任何东西可以隐藏,那么这将在您的实际git stash. 我不确定你是否可以在 Windows 上使用它。