如果我的 git 目录更改,我有一个 bash PS1 会显示红色,如果没有更改,我会显示绿色。
if [ $? -eq 0 ]; then \
echo "$(echo `git status` | grep "nothing to commit" > /dev/null 2>&1; \
if [ "$?" -eq "0" ]; then \
# @4 - Clean repository - nothing to commit
echo "\n'$Green'"$(__git_ps1 "(%s)"'$Color_Off'); \
else \
# @5 - Changes to working tree
echo "\n'$IRed'"$(__git_ps1 "(%s)"'$Color_Off'); \
fi)\$ "; \
fi)'
Run Code Online (Sandbox Code Playgroud)
这工作正常!但问题是在某些工作目录中非常慢,因为存在许多变化和很大的差异。
在没有完整的情况下获得 git status boolean (yes changed or no changed) 的更好方法是什么?
我试过git status --shortorgit status --porcelain但还是很慢。
也许这个答案可能有用:https ://stackoverflow.com/a/2659808/3903076
简而言之,您可以根据您的需要调整以下检查。有关更多详细信息,请参阅链接的答案。
if ! git diff-index --quiet --cached HEAD; then
# Index has changes.
exit 1;
elif ! git diff-files --quiet; then
# Working tree has changes.
exit 1;
elif [ -n "`git ls-files --others --exclude-standard`" ]; then
# There are untracked unignored files.
exit 1;
fi
Run Code Online (Sandbox Code Playgroud)