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


当前回答

我认为你可以使用以下命令:git reset——hard

其他回答

请注意,可能仍然有文件看起来不会消失——它们可能是未编辑的,但Git可能因为CRLF / LF更改而将它们标记为正在编辑。看看你最近是否在.gitattributes中做了一些更改。

在我的例子中,我将CRLF设置添加到.gitattributes文件中,因此所有文件都保留在“修改文件”列表中。更改.gitattributes设置会使它们消失。

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

git checkout filename

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

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

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

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