检测 Git 工作区中已修改文件的简单方法是什么?

发布于 2024-09-26 08:36:54 字数 147 浏览 5 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

荒岛晴空 2024-10-03 08:36:54

如果您只想要一个简单的“与 HEAD 有什么区别吗?”:

git diff-index --quiet HEAD

如果退出代码为 0,则没有区别。

如果您想要“哪些文件从 HEAD 发生了更改?”:

git diff-index --name-only HEAD

如果您想要“哪些文件从 HEAD 发生了更改,以及它们以什么方式更改(添加、删除、更改)?”:

git diff-index --name-status HEAD

添加 -M (和 -C)如果您想要重命名(和复制)检测。

这些命令将检查暂存的内容(索引中的内容)和工作树中的文件。像 git ls-files -m 这样的替代方案只会根据索引检查工作树(即它们将忽略工作树中的任何暂存(但未提交)内容)。

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).

一城柳絮吹成雪 2024-10-03 08:36:54

来自 git help -a :

git ls-files -m

From git help -a:

git ls-files -m
在巴黎塔顶看东京樱花 2024-10-03 08:36:54

git status --porcelain 似乎提供了一个很好的可解析输出。

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

执笔绘流年 2024-10-03 08:36:54

对于 git hooks,我发现这个命令很有用

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

例如

//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

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
坚持沉默 2024-10-03 08:36:54

git diff --name-only 做同样的事情(可能更直观......)

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

羁绊已千年 2024-10-03 08:36:54

尖角较少的方法:

git diff --quiet HEAD

这似乎与 diff-index 答案做同样的事情,但有一个优点:它会在必要时重建索引,消除 diff-index 的一些不稳定。此外,当存储库通过明显构建临时索引不可写时,diff可以正常工作(而diff-index,如果索引过时,会说所有内容都已修改,并且无法更新索引来解决这个问题)。

An approach with fewer sharp corners:

git diff --quiet HEAD

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 of diff-index. Additionally, diff works correctly when the repository isn't writable by apparently building a temporary index (while diff-index, if the index is stale, will say everything is modified, and the index can't be updated to fix that).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文