检测 Git 工作区中已修改文件的简单方法是什么?
在 make 过程中,我创建了嵌入链接输出中的字符串字段。非常有用。
除了复杂的 sed
/grep
解析 git status
命令之外,我如何轻松确定工作区中的文件是否已根据git ?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您只想要一个简单的“与 HEAD 有什么区别吗?”:
如果退出代码为 0,则没有区别。
如果您想要“哪些文件从 HEAD 发生了更改?”:
如果您想要“哪些文件从 HEAD 发生了更改,以及它们以什么方式更改(添加、删除、更改)?”:
添加
-M
(和-C
)如果您想要重命名(和复制)检测。这些命令将检查暂存的内容(索引中的内容)和工作树中的文件。像 git ls-files -m 这样的替代方案只会根据索引检查工作树(即它们将忽略工作树中的任何暂存(但未提交)内容)。
If you just want a plain “Are there any differences from HEAD?”:
If the exit code is 0, then there were no differences.
If you want “What files have changed from HEAD?”:
If you want “What files have changed from HEAD, and in what ways have they changed (added, deleted, changed)?”:
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).来自 git help -a :
From
git help -a
:git status --porcelain 似乎提供了一个很好的可解析输出。
git status --porcelain
seems to give a nice parsable output.对于 git hooks,我发现这个命令很有用
例如
For git hooks, I found this command useful
For example
git diff --name-only 做同样的事情(可能更直观......)
git diff --name-only
does the same (might be more intuitive...)尖角较少的方法:
这似乎与 diff-index 答案做同样的事情,但有一个优点:它会在必要时重建索引,消除 diff-index 的一些不稳定。此外,当存储库通过明显构建临时索引不可写时,
diff
可以正常工作(而diff-index
,如果索引过时,会说所有内容都已修改,并且无法更新索引来解决这个问题)。An approach with fewer sharp corners:
This appears to do the same thing as the
diff-index
answer, but with an advantage: it rebuilds the index if necessary, eliminating some flakiness ofdiff-index
. Additionally,diff
works correctly when the repository isn't writable by apparently building a temporary index (whilediff-index
, if the index is stale, will say everything is modified, and the index can't be updated to fix that).