是否有一个Git命令可以恢复工作树和索引中所有未提交的更改,并删除新创建的文件和文件夹?


当前回答

我通常使用这种方法,效果很好:

mv fold/file /tmp
git checkout fold/file

其他回答

git clean -fd

没有帮助,新的文件仍然存在。我完全删除了所有工作树,然后

git reset --hard

在“https://stackoverflow.com/questions/673407/how-do-i-clear-my-local-working-directory-in-git/673420#673420”中添加-x选项的建议:

git clean -fdx

注意-x标志将删除所有被Git忽略的文件,所以要小心(参见我引用的答案中的讨论)。

Git 2.23引入了Git restore命令来恢复工作树文件。

恢复当前目录下的所有文件。

git restore .

如果您想恢复所有C源文件以匹配索引中的版本,您可以这样做

git restore '*.c'

使用“git checkout——…”来丢弃工作目录中的更改

git checkout -- app/views/posts/index.html.erb

or

git checkout -- *

删除在git状态下对非暂存文件所做的所有更改

modified:    app/controllers/posts.rb
modified:    app/views/posts/index.html.erb

如果你有一个未提交的更改(它只在你的工作副本中),你希望恢复到最近提交的副本,请执行以下操作:

git checkout filename

我通常使用这种方法,效果很好:

mv fold/file /tmp
git checkout fold/file