Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-revert

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?

like image 766
Mujo Osmanovic Avatar asked Feb 13 '13 22:02

Mujo Osmanovic


People also ask

How do I revert uncommitted changes in git?

Try Git checkout --<file> to discard uncommitted changes to a file. Git reset --hard is for when you want to discard all uncommitted changes. Use Git reset --hard <commit id> to point the repo to a previous commit.

How do you dispose of uncommitted changes?

Use git reset to Remove Uncommitted Changes in Git Another way to remove uncommitted changes using git reset is with option --hard and params HEAD . The --hard option specifies Git to throw ALL changes between the current state and the commit in the last argument.


2 Answers

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!

like image 89
CharlesB Avatar answered Sep 28 '22 06:09

CharlesB


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.

like image 22
Mujo Osmanovic Avatar answered Sep 28 '22 05:09

Mujo Osmanovic