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

ぃ、小莉子 提交于 2019-11-29 01:44:57

问题


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?


回答1:


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

git checkout -- "*.xml"

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

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

set -f
git checkout -- *.xml

This, of course, will irremediably erase your changes!




回答2:


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.




回答3:


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

or something equivalent if your system doesn't have find and xargs. Or just git checkout HEAD **/*.xml in zsh or any other shell with this form of reqursive globbing.



来源:https://stackoverflow.com/questions/14864655/how-to-revert-uncommitted-changes-to-files-of-a-certain-type-in-git

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!