开发者

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

开发者 https://www.devze.com 2023-04-04 14:51 出处:网络
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:

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 che开发者_Python百科ckout on each file separately?


Try this:

$ git checkout `git ls-files -m`

-m lists only the modified files.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消