目標是獲得可以在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
還有其他方法嗎?
更新,現在這個解決看到了馬克Longair後
我嘗試這樣做,但是會導致出現問題。
if [ -z $(git status --porcelain) ];
then
echo "IT IS CLEAN"
else
echo "PLEASE COMMIT YOUR CHANGE FIRST!!!"
echo git status
fi
當
它會得到一些錯誤,[:??:二進制運算符預期
現在,我在看的人,嘗試git的差異。
===================代碼爲我的希望,希望更好的回答================== ====
#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
我已經更新了問題標題。讓我知道這是你的意思。 – 2011-02-28 07:47:38
在更新的部分,似乎你實際上並沒有做[eckes](http://stackoverflow.com/users/520162/eckes)在[他的回答]中提出的建議(http://stackoverflow.com/questions/5139290/how-to-check-if-theres-nothing-to-commit-in-the-current-branch/5139346#5139346) - 正如他所說的,你需要在'$( git status --porcelain)'。另外,如果你想在你的消息中加上驚歎號,你需要使用單引號而不是雙引號 - 也就是說,它應該是'echo'請改變你的改變!''而不是 – 2011-02-28 10:13:25
,因爲Mark說:* *您需要在'$(git status --porcelain)'**周圍加雙引號,就像我告訴過你的! – eckes 2011-02-28 10:28:19