10
votes

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?

3
If you do git checkout -- <path> (the slightly safer version of git checkout whole_folder) the timestamps remain the same for files that aren't changed by that command. That means that your build process should still only be rebuilding the the files that it minimally thinks it needs to based on mtimes.Mark Longair
Are you sure that git checks out everything? IIRC git tries really hard to check out only files that are modified and not touch anything else when you do git checkout -- whole_folder, or git reset --hard HEAD.holygeek
So, I should do git checkout -- whole_folder ?Geo
Yes, I think that what you're doing is correct - if you see timestamps being updated for files that git didn't need to change, that's a bug, I think. (I've added an answer to that effect.)Mark Longair

3 Answers

18
votes

Try this:

$ git checkout `git ls-files -m`

-m lists only the modified files.

6
votes

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

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.