Geo
Geo

Reputation: 96787

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

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?

Upvotes: 10

Views: 14854

Answers (3)

TheFox
TheFox

Reputation: 502

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-)

Upvotes: 6

Mark Longair
Mark Longair

Reputation: 467141

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

git checkout -- whole_folder

git will only update the timestamps on files that it actually needs to change, so if your dependency-based build tool is using mtimes correctly, the minimal safe number of files should be rebuilt. If you're seeing different behaviour, that would be a bug.

Upvotes: 2

holygeek
holygeek

Reputation: 16185

Try this:

$ git checkout `git ls-files -m`

-m lists only the modified files.

Upvotes: 19

Related Questions