目标是获得可以在 shell 命令中评估的明确状态。
我试过git status
了,但它总是返回 0,即使有要提交的项目。
git status
echo $? #this is always 0
我有一个想法,但我认为这是一个坏主意。
if [ git status | grep -i -c "[a-z]"> 2 ];
then
code for change...
else
code for nothing change...
fi
还有其他方法吗?
更新以下解决方案,请参阅 Mark Longair 的帖子
我试过这个,但它会导致一个问题。
if [ -z $(git status --porcelain) ];
then
echo "IT IS CLEAN"
else
echo "PLEASE COMMIT YOUR CHANGE FIRST!!!"
echo git status
fi
我收到以下错误[: ??: binary operator expected
现在,我正在看那个人并尝试 git diff。
===================我希望的代码,希望更好的答案======================
#if [ `git status | grep -i -c "$"` -lt 3 ];
# change to below code,although the above code is simple, but I think it is not strict logical
if [ `git diff --cached --exit-code HEAD^ > /dev/null && (git ls-files --other --exclude-standard --directory | grep -c -v '/$')` ];
then
echo "PLEASE COMMIT YOUR CHANGE FIRST!!!"
exit 1
else
exit 0
fi