39

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mujo Osmanovic
  • 931
  • 2
  • 11
  • 15

3 Answers3

69

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!

CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • 3
    `*/*.xml` is not "xml files in all directories" either. – wRAR Feb 13 '13 at 22:59
  • @wRAR found it... globbing in git isn't very well documented – CharlesB Feb 13 '13 at 23:09
  • 1
    `**/*.xml` will use your shell globs, so files deleted won't be checked out, but if it's changes only, that should work. – Reactormonk Feb 13 '13 at 23:25
  • Actually it doesn't uses shell globs, but git understands globs (I checked by disabling shell glob expansion with `set -f` in bash), and it works also for restoring deleted files – CharlesB Feb 13 '13 at 23:29
  • 1
    For some reason, this doesn't work when I specify a branch :-( `git checkout master -- "*.xml"` for example doesn't work where `git checkout -- "*.xml"` does :-( – mjaggard May 22 '17 at 10:08
  • @mjaggard cuz branch goes in place of `--`, i.e. `git checkout master "*.xml"` – SMSk Nov 20 '17 at 15:22
  • @mjaggard When you specify branch then behavior is indeed different. It doesn't takes files recursively :/. You can look at my other [answer](https://stackoverflow.com/a/53952571/350384) to see how to do it differently. – Mariusz Pawelski Dec 28 '18 at 01:02
  • 1
    Is there a way to checkout -- files that are not a certain type? like git checkout -- "!*.xml" – sdfsdf Jan 10 '19 at 19:51
  • Is there something like _--dry-run_ for merge to **simulate** this before actually doing it, to see which files would be checked out? – Steak Overflow Jan 17 '19 at 10:03
  • @CharlesB How to do reverse, with simple and short changes to your code, i.e. how to restore only all modified files that doesn't match *.xml or better yet doesn't match *.xml and *.php ?? – Vicky Dev Apr 25 '23 at 13:17
  • @VickyDev `git checkout -- ':!*.xml'` For not matching xml and php I'm not sure – CharlesB May 11 '23 at 08:34
  • @CharlesB .php is also a type of text file like .xml, so in nutshell I am just asking on **how to do this for not matching multiple extensions** ? – Vicky Dev May 17 '23 at 16:02
5

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.

Mujo Osmanovic
  • 931
  • 2
  • 11
  • 15
  • see my answer, you don't need all of this :) – CharlesB Feb 13 '13 at 23:42
  • How to do reverse, with simple and short changes to your code, i.e. how to restore only all modified files that doesn't match *.xml or better yet doesn't match *.xml and *.php ?? – Vicky Dev Apr 25 '23 at 13:17
1
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.

wRAR
  • 25,009
  • 4
  • 84
  • 97