git 检查git存储库中的特定文件是否已更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17797740/
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
Check if specific file in git repository has changed
提问by user2554585
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 的新手,但我有一个存储库,其中包含一个名为“test”的文件。我想检查该特定文件是否已更改。有没有办法做到这一点?
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)
从大局来看,我正在编写一个批处理文件,如果有任何更改(我已经使用试运行选项发现),除了测试文件(这意味着即使测试文件已更改,我不想执行 git clone)
Let me know if you need any clarifications and thanks for your time
如果您需要任何说明,请告诉我并感谢您的时间
回答by Peter Westmacott
You can pass a file or directory name to git diff
to see only changes to that file or directory.
您可以传递文件或目录名称以git diff
仅查看对该文件或目录的更改。
If you're "writing a batch file" then the --exit-code
switch to git diff
may be particularly useful. eg.
如果您正在“编写批处理文件”,那么--exit-code
切换到git diff
可能特别有用。例如。
git diff --exit-code test
git diff --exit-code test
or:
或者:
git diff --exit-code path/to/source/dir
git diff --exit-code path/to/source/dir
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.)
如果命名的文件test
(或任何其他指定的文件或目录)发生更改,则返回 1,否则返回0。(允许脚本对结果进行分支。)
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.
要仅查看已更改文件的名称(而不是完整的差异),请使用--name-only
xor,您可以使用-s
它根本不获取任何输出,而只获取退出代码。
回答by GoZoner
Using
使用
git diff test
will show the differences between the work directory test
and the repository version. Using diff
will show the actual differences; if you are not interested in those use
将显示工作目录test
和存储库版本之间的差异。使用diff
将显示实际差异;如果您对这些用途不感兴趣
git diff --name-only test
回答by Peter Lundgren
Git doesn't provide methods to query history of a remote repository. Git commands are intended to be run against a local repository, so you would have to clone
first (fetch
would be cheaper if you've cloned once before). That said, there are some ways to get around this limitation:
Git 不提供查询远程存储库历史记录的方法。Git 命令旨在针对本地存储库运行,因此您必须clone
首先(fetch
如果您之前克隆过一次会更便宜)。也就是说,有一些方法可以绕过这个限制:
- You could ask your Git server to run the commands you want for you via some kind of API. For example, browsing GitHub webpages or using their developer APIfall into this category. In this case, GitHub's web servers are running Git commands for you. If you're using a server other than bare Git, check to see if your server has an API that could help.
- Use git-archiveto download an archive containing parts of the repository. I don't think this will help you.
- 你可以让你的 Git 服务器通过某种 API 运行你想要的命令。例如,浏览 GitHub 网页或使用他们的开发人员 API 就属于这一类。在这种情况下,GitHub 的 Web 服务器正在为您运行 Git 命令。如果您使用的不是裸 Git 的服务器,请检查您的服务器是否有可以提供帮助的 API。
- 使用git-archive下载包含部分存储库的存档。我不认为这会帮助你。
回答by GoodDok
As it was correctly mentioned in the answer of Peter Lundgren,
正如Peter Lundgren的回答中正确提到的那样,
git commands are intended to be run against a local repository,
git 命令旨在针对本地存储库运行,
so git clone
is likely to be called anyways.
所以git clone
很可能会被调用。
On the other hand, if you need to check if you want to trigger some specific CI step, you might find useful something like this in your script:
另一方面,如果您需要检查是否要触发某些特定的 CI 步骤,您可能会在脚本中发现类似这样的有用内容:
if git diff --quiet $COMMIT_HASH^! -- . ':!test'; then
echo "No significant changes"
else
echo "There are some significant changes, let's trigger something..."
fi
--quiet
disables all output of the program and implies --exit-code
(see git documentation).
--quiet
禁用程序的所有输出并暗示--exit-code
(参见git 文档)。
Reference this answerfor more details regarding the pattern at the end of the expression.
有关表达式末尾模式的更多详细信息,请参考此答案。