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


当前回答

安全而漫长的路:

Git分支要删除 Git签出删除 Git添加。 “我做了件坏事,对不起” Git检查开发 git分支-D删除

其他回答

你可以使用下面的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

从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)

Use:

git reset HEAD filepath

例如:

git reset HEAD om211/src/META-INF/persistence.xml

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

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

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忽略的文件,所以要小心(参见我引用的答案中的讨论)。