10

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?

Geo
  • 93,257
  • 117
  • 344
  • 520
  • If you do `git checkout -- ` (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 Sep 14 '11 at 11:54
  • 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 Sep 14 '11 at 12:00
  • So, I should do `git checkout -- whole_folder` ? – Geo Sep 14 '11 at 12:01
  • 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 Sep 14 '11 at 12:16

3 Answers3

19

Try this:

$ git checkout `git ls-files -m`

-m lists only the modified files.

holygeek
  • 15,653
  • 1
  • 40
  • 50
  • Isn't there a cross-platform solution available? I am sometimes using Windows from a standard `cmd.exe` shell. – Geo Sep 14 '11 at 11:03
6

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-)
TheFox
  • 502
  • 10
  • 26
  • Thanks @TheFox. It almost worked. It had to modify the command about a bit to make it work for me. Here is the line I used: ```git checkout -- $(git status | grep modified: | cut -c14-)``` – user1029978 Oct 24 '17 at 07:46
2

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.

Mark Longair
  • 446,582
  • 72
  • 411
  • 327