如何在 git 中恢复对特定类型文件的未提交更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14864655/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:10:36  来源:igfitidea点击:

How to revert uncommitted changes to files of a certain type in git

gitgit-revert

提问by Mujo Osmanovic

I have a bunch of modified files in my git repository and a large number of them are xml files. How do I revert changes (reset modifications) of only the xml files?

我的 git 存储库中有一堆修改过的文件,其中很多是 xml 文件。如何仅还原 xml 文件的更改(重置修改)?

采纳答案by Mujo Osmanovic

Thank you all for your replies, but I have found, for me, most accurate solution:

谢谢大家的回复,但我找到了,对我来说,最准确的解决方案:

git diff --name-only -- '*.xml' | sed 's, ,\&,g' | xargs git checkout --

sed is user to escape spaces which troubled xargs and everything is working very fast and accurate.

sed 是逃避困扰 xargs 的空间的用户,并且一切都非常快速和准确。

回答by CharlesB

You don't need findor sed, you can use wildcards as git understands them (doesn't depend on your shell):

你不需要findor sed,你可以使用通配符,因为 git 理解它们(不依赖于你的 shell):

git checkout -- "*.xml"

The quotes will prevent your shell to expand the command to only files in the current directory before its execution.

引号将阻止您的 shell 在执行之前将命令扩展到当前目录中的文件。

You can also disable shell glob expansion (with bash) :

您还可以禁用 shell glob 扩展(使用 bash):

set -f
git checkout -- *.xml

This, of course, will irremediably erase your changes!

当然,这将无可挽回地抹去您的更改!

回答by wRAR

find . -name '*.xml' -print0 | xargs -0 git checkout HEAD

or something equivalent if your system doesn't have findand xargs. Or just git checkout HEAD **/*.xmlin zshor any other shell with this form of reqursive globbing.

或者如果您的系统没有find和等效的东西xargs。或者只是git checkout HEAD **/*.xmlzsh或任何其他具有这种形式的 reqursive globbing 的 shell 中。