git 如何恢复未提交的更改,包括文件和文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5807137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:47:45  来源:igfitidea点击:

How to revert uncommitted changes including files and folders?

gitgit-resetgit-revertgit-clean

提问by MEM

Is there a git command to revert all uncommitted changes in a working tree and index and to also remove newly created files and folders?

是否有 git 命令来恢复工作树和索引中所有未提交的更改并删除新创建的文件和文件夹?

回答by htanata

You can run these two commands:

您可以运行这两个命令:

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

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

回答by Ramashish Baranwal

If you want to revert the changes only in current working directory, use

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

git checkout -- .

And before that, you can list the files that will be reverted without actually making any action, just to check what will happen, with:

在此之前,您可以列出将要还原的文件,而无需实际执行任何操作,只需检查会发生什么,使用:

git checkout --

回答by Zarne Dravitzki

Use "git checkout -- ..." to discard changes in working directory

使用 "git checkout -- ..." 丢弃工作目录中的更改

git checkout -- app/views/posts/index.html.erb

or

或者

git checkout -- *

removes all changes made to unstaged files in git status eg

删除对 git status 中未暂存文件所做的所有更改,例如

modified:    app/controllers/posts.rb
modified:    app/views/posts/index.html.erb

回答by glumgold

One non-trivial way is to run these two commands:

一种重要的方法是运行这两个命令:

  1. git stashThis will move your changes to the stash, bringing you back to the state of HEAD
  2. git stash dropThis will delete the latest stash created in the last command.
  1. git stash这会将您的更改移至 stash,让您回到 HEAD 的状态
  2. git stash drop这将删除在最后一个命令中创建的最新存储。

回答by Fr0sT

git clean -fd

didn't help, new files remained. What I did is totally deleting all the working tree and then

没有帮助,新文件仍然存在。我所做的是完全删除所有工作树,然后

git reset --hard

See "How do I clear my local working directory in git?" for advice to add the -xoption to clean:

请参阅“如何清除 git 中的本地工作目录?”以获取添加-x清理选项的建议:

git clean -fdx

Note-xflag will remove all files ignored by Git so be careful (see discussion in the answer I refer to).

注意-x标志将删除 Git 忽略的所有文件,所以要小心(请参阅我参考的答案中的讨论)。

回答by Josnidhin

I think you can use the following command: git reset --hard

我认为您可以使用以下命令: git reset --hard

回答by Rob

Please note that there might still be files that won't seem to disappear - they might be unedited, but git might have marked them as being edited because of CRLF / LF changes. See if you've made some changes in .gitattributesrecently.

请注意,可能仍有一些文件似乎不会消失——它们可能未经编辑,但由于 CRLF / LF 更改,git 可能已将它们标记为正在编辑。看看你.gitattributes最近是否做了一些改变。

In my case I've added CRLF settings into the .gitattributesfile and all the files remained in the "modified files" list because of this. Changing the .gitattributes settings made them disappear.

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

回答by kgandroid

If you have an uncommitted change (its only in your working copy) that you wish to revert to the copy in your latest commit, do the following:

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

git checkout filename

回答by TheKojuEffect

Git 2.23 introduced git restorecommand to restore working tree files.

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

https://git-scm.com/docs/git-restore

https://git-scm.com/docs/git-restore

To restore all files in the current directory

恢复当前目录中的所有文件

git restore .

git恢复。

If you want to restore all C source files to match the version in the index, you can do

如果要恢复所有 C 源文件以匹配索引中的版本,可以执行

git restore '*.c'

git恢复'*.c'

回答by Haritsinh Gohil

You can just use following git command which can revert back all the uncommitted changes made in your repository:

您只需使用以下 git 命令即可恢复存储库中所有未提交的更改:

git checkout .

Example:

例子:

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