检查git存储库中的特定文件是否已更改 [英] Check if specific file in git repository has changed

查看:164
本文介绍了检查git存储库中的特定文件是否已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是git的新手,但是我有一个包含名为"test"的文件的存储库.我想检查该特定文件是否已更改.反正有这样做吗?

I am novice in dealing with git, but I have a repository that contains a file named 'test'. I want to check if that specific file has changed. Is there anyway to do that?

从大图上看,我正在编写一个批处理文件,如果发生任何更改(我使用空运行选项确定了),它将对存储库执行git clone(测试文件除外)(这意味着即使测试文件已更改,我不想执行git clone)

From a big picture, I'm writing a batch file that will perform a git clone of the repository if anything has changed (which I have figured out using the dry run option) EXCEPT for the test file(meaning that even if the test file has changed, I don't want to perform a git clone)

让我知道您是否需要任何澄清,并感谢您的时间

Let me know if you need any clarifications and thanks for your time

推荐答案

您可以将文件或目录名称传递给git diff,以仅查看对该文件或目录的更改.

You can pass a file or directory name to git diff to see only changes to that file or directory.

如果您正在编写批处理文件",则将--exit-code切换到git diff可能特别有用.例如.

If you're "writing a batch file" then the --exit-code switch to git diff may be particularly useful. eg.

git diff --exit-code test

或:

git diff --exit-code path/to/source/dir

如果对名为test的文件(或任何其他指定的文件或目录)进行了更改,则返回1,否则返回0. (允许脚本分支到结果上.)

Which will return 1 if there are changes to the file named test (or any other specified file or directory) or 0 otherwise. (Allowing a script to branch on the result.)

要仅查看已更改文件的名称(而不是完整的差异),请使用--name-only x或使用-s根本不输出任何信息,而仅获得退出代码.

To see the names of the changed files only (and not a complete diff) use --name-only xor you can use -s to get no output at all, but just the exit code.

这篇关于检查git存储库中的特定文件是否已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆