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


当前回答

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

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

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

其他回答

从Git帮助:

 Changes to be committed:
      (use "git restore --staged <file>..." to unstage)

    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)

使用“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分支要删除 Git签出删除 Git添加。 “我做了件坏事,对不起” Git检查开发 git分支-D删除

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

git checkout filename

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

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

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