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


当前回答

一个不平凡的方法是运行这两个命令:

这将移动您的更改到stash,将您带回HEAD状态 这将删除在上一个命令中创建的最新的stash。

其他回答

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

git checkout -- .

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

git checkout --

你可以使用下面的Git命令,它可以恢复你仓库中所有未提交的更改:

git checkout .

例子:

ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

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

        modified:   application/controllers/Drivers.php
        modified:   application/views/drivers/add.php
        modified:   application/views/drivers/load_driver_info.php
        modified:   uploads/drivers/drivers.xlsx

no changes added to commit (use "git add" and/or "git commit -a")

ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git checkout .

ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working tree clean

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

# 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 2.23引入了Git restore命令来恢复工作树文件。

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

git restore .

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

git restore '*.c'