How to recover only changed files in git camera?

Let's say I have a directory containing hundreds of files. I modify a few of them, but then I realize that my changes are bad. If I do this:

git checkout whole_folder 

Then everything is checked again, and I have to recompile everything. Is there a way to make a checkout affect only changed files, or do I need to run a checkout for each file separately?

+7
source share
3 answers

Try the following:

 $ git checkout `git ls-files -m` 

-m lists only modified files.

+13
source

What you are doing is correct, but you might want to add disambiguating -- just in case you have a directory name, the same as a branch name, i.e.

 git checkout -- whole_folder 

git will only update timestamps on files that actually need to be changed, so if your dependency tool uses mtimes correctly, the minimum safe number of files should be rebuilt. If you see other behavior, this will be a mistake.

+2
source

But

 git checkout -- $(git ls-files -m) 

also checks deleted files.

If you want to check only modified files, this works for me:

 git checkout -- $(git status -uno | grep --colour=never '#' | awk '{ print $2 $3 }' | grep --colour=never ^modified: | cut -c10-) 
+2
source

All Articles