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


当前回答

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

mv fold/file /tmp
git checkout fold/file

其他回答

使用“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 reset——hard

如果您只想还原当前工作目录中的更改,请使用

git checkout -- .

在此之前,你可以列出将被恢复的文件,而不实际做任何操作,只是检查将会发生什么,用:

git checkout --

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

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

git restore .

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

git restore '*.c'

您可以执行以下两条命令:

# Revert changes to modified files.
git reset --hard

# Remove all untracked files and directories.
# '-f' is force, '-d' is remove directories.
git clean -fd