我如何检查我的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或更新版本。

其他回答

一个DIY的可能性,更新遵循0xfe的建议

#!/bin/sh
exit $(git status --porcelain | wc -l) 

正如Chris Johnsen所指出的,这只适用于Git 1.7.0或更新版本。

看了一下这些答案…(在*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的分享

VonC答案的实现:

if [[ -n $(git status --porcelain) ]]; then echo "repo is dirty"; fi

这个帖子可能会有更好的答案组合。但这对我有用……对于你的.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
}