如何使“git描述”提及是否存在局部变化?
如何在脚本中检查本地更改是否存在?也许与gitdescribe
结合使用?
How can I check, in a script, if local changes are present? Perhaps in combination with git describe
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 git 1.6.6 开始,
git-describe
接受了--dirty
选项。如果您的工作树中有未提交的更改,那么您将得到如下输出:Since git 1.6.6,
git-describe
has accepted a--dirty
option. If you have uncommitted changes in your working tree then you'll get output like this:您需要确保满足以下两个属性:
HEAD 和索引缓存之间没有差异
git diff-index --cached HEAD
索引和工作树之间没有区别:
git diff-files
两个命令都采用
- -quiet
参数,将根据是否存在差异设置退出代码(从 git 1.4 之后的某个时间开始)。如果您需要使其在 git 1.4 上运行,则需要运行不带--quiet
的命令并检查它们是否产生任何输出。注意:
git diff
是一个陶瓷命令,因此不应在脚本中使用。请改用上面的管道命令。示例 shell 代码,取自我的 git_version.sh 脚本< /a>:
如果你可以要求 git 版本 >= 1.5,
if git diff-files --quiet && git diff-index --quiet --cached HEAD;那么
可以代替上面的比较。注意:此解决方案(与 Antony 的交互式
git diff HEAD --quiet
完全相同)仅发现相对于 HEAD 的本地更改。然而,本地提交也可以被认为是本地更改,并且自然不会出现在与 HEAD 的任何差异中。您需要检查gitdescribe
使用的SHA1值来检测HEAD是否来自您认为不是本地更改的一组提交。You will need to make sure that both the two following properties are met:
That there are no differences between HEAD and the index cache
git diff-index --cached HEAD
That there are no differences between the index and the working tree:
git diff-files
Both commands take a
--quiet
parameter which will set the exit code according to whether there are differences or not (starting some time after git 1.4). If you need to make it work on git 1.4, you need to run the commands without--quiet
and check whether they produce any output.Note:
git diff
is a porcelain command, and thus should not be used in scripts. Use above plumbing commands instead.Example shell code, taken from my git_version.sh script:
If you can require a git version >= 1.5,
if git diff-files --quiet && git diff-index --quiet --cached HEAD; then
can replace above comparison.Note: This solution (exactly like Antony's interactive
git diff HEAD --quiet
) only discovers local changes relative to HEAD. However, local commits can arguable also be considered local changes, and naturally will not show up in any diff against HEAD. You will need to check the SHA1 valuegit describe
uses to detect whether HEAD is from a set of commits you consider not to be local changes.如果有更改,
git diff --quiet
返回退出状态 1,如果没有,则返回 0。请记住,它将显示分阶段更改和工作目录之间的差异。
如果您对 HEAD 和工作目录之间的更改感兴趣,您应该使用 git diff HEAD --quiet 。
--quiet
意味着--exit-code
。git diff --quiet
returns with exit status 1 if there're changes and 0 if there're not.Keep in mind that it will show diff between staged changes and the working dir.
If you're interested in changes between your HEAD and working dir, you should use
git diff HEAD --quiet
.--quiet
implies--exit-code
.如果没有本地更改,
git status
将以非零状态退出。但我不明白你的意思“与 git describe 结合”。
git status
exits with non-zero status if there is no local changes.But I don't understand what you mean "in combination with git describe".