在 Git 工作区中检测修改文件的简单方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3882838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's an easy way to detect modified files in a Git workspace?
提问by Jamie
During make, I create string fields which I embedded in the linked output. Very useful.
在 make 期间,我创建了嵌入在链接输出中的字符串字段。很有用。
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
?
除了命令的复杂sed
/grep
解析之外git status
,如何轻松确定工作区中的文件是否已根据git
?
回答by Chris Johnsen
If you just want a plain “Are there any differences from HEAD?”:
如果你只是想要一个简单的“与 HEAD 有什么区别吗?”:
git diff-index --quiet HEAD
If the exit code is 0, then there were no differences.
如果退出代码为 0,则没有区别。
If you want “What files have changed from HEAD?”:
如果您想要“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)?”:
如果您想“从 HEAD 更改了哪些文件,它们以哪些方式更改(添加、删除、更改)?”:
git diff-index --name-status HEAD
Add -M
(and -C
) if you want rename (and copy) detection.
如果要重命名(和复制)检测,请添加-M
(和-C
)。
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).
这些命令将检查暂存内容(索引中的内容)和工作树中的文件。像这样的替代方案git ls-files -m
只会根据索引检查工作树(即它们将忽略也在工作树中的任何暂存(但未提交)的内容)。
回答by Christoffer Hammarstr?m
git ls-files -m
git ls-files -m
To find this you could have browsed through git help -a
.
要找到它,您可以浏览git help -a
.
回答by MDCore
git status --porcelain
seems to give a nice parsable output.
git status --porcelain
似乎给出了一个很好的可解析输出。
回答by Maragues
For git hooks, I found this command useful
对于 git hooks,我发现这个命令很有用
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
回答by Yinon Ehrlich
git diff --name-only
does the same (might be more intuitive...)
git diff --name-only
做同样的事情(可能更直观......)