Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I restore only the modified files on a git checkout?

Tags:

git

Say I have a directory containing hundreds of files. I modify several of them, but afterwards I realize my changes are bad. If I do:

git checkout whole_folder

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

like image 551
Geo Avatar asked Sep 14 '11 10:09

Geo


People also ask

Can I recover from git checkout?

No, you can't. All these commands deal with committed history.

Does git checkout change files?

The git checkout command can be used in a commit, or file level scope. A file level checkout will change the file's contents to those of the specific commit.


2 Answers

Try this:

$ git checkout `git ls-files -m`

-m lists only the modified files.

like image 123
holygeek Avatar answered Sep 30 '22 13:09

holygeek


But

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

also checksout the deleted files.

If you want to checkout only the modified files this work for me:

git checkout -- $(git status -uno | grep --colour=never '#' | awk '{ print $2 $3 }' | grep --colour=never ^modified: | cut -c10-)
like image 27
TheFox Avatar answered Sep 30 '22 14:09

TheFox