简体   繁体   English

如何在git checkout上仅恢复修改后的文件?

[英]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? 有没有办法让checkout只影响修改过的文件,还是我需要分别对每个文件运行checkout

Try this: 试试这个:

$ git checkout `git ls-files -m`

-m lists only the modified files. -m仅列出已修改的文件。

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, ie: 你正在做的是正确的,但你可能想要添加消除歧义--以防你有一个与分支名称相同的目录名,即:

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. git只会更新它实际需要更改的文件的时间戳,因此如果基于依赖项的构建工具正确使用mtimes,则应重建最小安全文件数。 If you're seeing different behaviour, that would be a bug. 如果您看到不同的行为,那将是一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM