What's an easy way to detect modified files in a Git workspace?

偶尔善良 提交于 2019-12-30 03:39:25

问题


During make, I create string fields which I embedded in the linked output. Very useful.

Other than a complex sed/grep parsing of the git status command, how can I easily determine if files in the workspace have been modified according to git?


回答1:


If you just want a plain “Are there any differences from HEAD?”:

git diff-index --quiet HEAD

If the exit code is 0, then there were no differences.

If you want “What files have changed from HEAD?”:

git diff-index --name-only HEAD

If you want “What files have changed from HEAD, and in what ways have they changed (added, deleted, changed)?”:

git diff-index --name-status HEAD

Add -M (and -C) if you want rename (and copy) detection.

These commands will check both the staged contents (what is in the index) and the files in the working tree. Alternatives like git ls-files -m will only check the working tree against the index (i.e. they will disregard any staged (but uncommitted) content that is also in the working tree).




回答2:


git ls-files -m

To find this you could have browsed through git help -a.




回答3:


git status --porcelain seems to give a nice parsable output.




回答4:


git diff --name-only does the same (might be more intuitive...)




回答5:


For git hooks, I found this command useful

git diff-index --exit-code --ignore-submodules HEAD

For example

//run some static analysis check that can change the code
something_changed=`git diff-index --exit-code --ignore-submodules HEAD`
if [ -n "$something_changed" ]
then
    echo >&2 "Something changed in $local_ref, not pushing"
    exit 1
fi


来源:https://stackoverflow.com/questions/3882838/whats-an-easy-way-to-detect-modified-files-in-a-git-workspace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!