我如何检查我的git存储库中是否有任何未提交的更改:
添加到索引但未提交的更改 无路径的文件
从一个脚本?
git-status在git 1.6.4.2版本中似乎总是返回0。
我如何检查我的git存储库中是否有任何未提交的更改:
添加到索引但未提交的更改 无路径的文件
从一个脚本?
git-status在git 1.6.4.2版本中似乎总是返回0。
当前回答
一个DIY的可能性,更新遵循0xfe的建议
#!/bin/sh
exit $(git status --porcelain | wc -l)
正如Chris Johnsen所指出的,这只适用于Git 1.7.0或更新版本。
其他回答
为什么不封装'git状态与一个脚本:
将分析该命令的输出 是否会根据需要返回适当的错误代码
这样,您就可以在脚本中使用“增强”状态。
正如0xfe在他的精彩回答中提到的,git状态—瓷器在任何基于脚本的解决方案中都是重要的
--porcelain
为脚本提供稳定、易于解析的输出格式。 目前这与——short输出相同,但保证将来不会更改,因此对脚本是安全的。
这个帖子可能会有更好的答案组合。但这对我有用……对于你的.gitconfig的[alias]部分…
# git untracked && echo "There are untracked files!"
untracked = ! git status --porcelain 2>/dev/null | grep -q "^??"
# git unclean && echo "There are uncommited changes!"
unclean = ! ! git diff --quiet --ignore-submodules HEAD > /dev/null 2>&1
# git dirty && echo "There are uncommitted changes OR untracked files!"
dirty = ! git untracked || git unclean
这是最好、最干净的方法。由于某些原因,所选的答案对我不起作用,它没有拾取未提交的新文件所进行的更改。
function git_dirty {
text=$(git status)
changed_text="Changes to be committed"
untracked_files="Untracked files"
dirty=false
if [[ ${text} = *"$changed_text"* ]];then
dirty=true
fi
if [[ ${text} = *"$untracked_files"* ]];then
dirty=true
fi
echo $dirty
}
你也可以
git describe --dirty
. 如果它检测到一个肮脏的工作树,它将在结尾附加单词“-dirty”。根据git-describe(1):
--dirty[=<mark>]
Describe the working tree. It means describe HEAD and appends <mark> (-dirty by default) if
the working tree is dirty.
. 注意:未跟踪的文件不被认为是“脏文件”,因为,正如manpage声明的那样,它只关心工作树。
看了一下这些答案…(在*nix和windows上有各种问题,这是我的要求)…发现以下方法效果很好…
git diff --no-ext-diff --quiet --exit-code
检查*nix中的退出代码
echo $?
#returns 1 if the repo has changes (0 if clean)
查看窗口$中的退出代码
echo %errorlevel%
#returns 1 if the repos has changes (0 if clean)
来源:https://github.com/sindresorhus/pure/issues/115 感谢@paulirish的分享