一、git diff
diffInfo=`git diff` if [ "$diffInfo" = "" ]; then echo '本地没有变化无需提交' exit fi
二、git status
On branch feature/20191218-songyongzhan-test Your branch is up to date with 'origin/feature/20191218-songyongzhan-test'. Untracked files: (use "git add <file>..." to include in what will be committed) src/Map/RedisKey/Keys/vvv.php nothing added to commit but untracked files present (use "git add" to track)
这两种方式都能检测到本地是否发生变化,
这里推荐使用第二种方式 git status
区别:
git status 在新增文件后,可以检测到 nothing added to commit 我们可以得到这样的信息
git diff 不会得到任何的信息,git diff 只能检测出已经存在的文件发生变化,才能检测出。
lastMessage=`git status | tail -n 2` #nothing to commit, working tree clean 本地没有变化 noCommit='nothing to commit' if [[ $lastMessage =~ $noCommit ]]; then echo "无需提交" exit fi