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?

4
  • 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. Commented Sep 14, 2011 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. Commented Sep 14, 2011 at 12:00
  • So, I should do git checkout -- whole_folder ? Commented Sep 14, 2011 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.) Commented Sep 14, 2011 at 12:16

3 Answers 3

19

Try this:

$ git checkout `git ls-files -m` 

-m lists only the modified files.

Sign up to request clarification or add additional context in comments.

1 Comment

Isn't there a cross-platform solution available? I am sometimes using Windows from a standard cmd.exe shell.
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-) 

1 Comment

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.