如何恢复未提交的更改(包括文件和文件夹)?
是否有 Git 命令可以恢复工作树和索引中所有未提交的更改,并删除新创建的文件和文件夹?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您可以运行这两个命令:
You can run these two commands:
如果您只想恢复当前工作目录中的更改,请使用
在此之前,您可以列出将恢复的文件,而无需实际执行任何操作,只是为了检查会发生什么,使用:
If you want to revert the changes only in the current working directory, use
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 -- ...”放弃工作目录中的更改
或
删除 git status 中对未暂存文件所做的所有更改,例如
Use "git checkout -- ..." to discard changes in working directory
or
removes all changes made to unstaged files in git status eg
一种重要的方法是运行这两个命令:
git stash
这会将您的更改移动到存储中,使您回到 HEAD 的状态git stash drop
这将删除在最后一个命令中创建的最新存储。One non-trivial way is to run these two commands:
git stash
This will move your changes to the stash, bringing you back to the state of HEADgit stash drop
This will delete the latest stash created in the last command.Git 2.23 引入了
git Restore
命令来恢复工作树文件。恢复当前目录中的所有文件:
如果要恢复所有C源文件以匹配索引中的版本,可以这样做
Git 2.23 introduced the
git restore
command to restore working tree files.To restore all files in the current directory:
If you want to restore all C source files to match the version in the index, you can do
没有帮助,新文件仍然存在。我完全删除了所有工作树,然后
参见“https://stackoverflow.com/questions/673407/how-do-i-clear-my-local-working-directory-in-git/673420#673420” 添加
-x
选项进行清理的建议:注意
-x
标志将删除 Git 忽略的所有文件,因此小心(参见我参考的答案中的讨论)。didn't help, and new files remained. I totally deleted all the working tree and then
See "https://stackoverflow.com/questions/673407/how-do-i-clear-my-local-working-directory-in-git/673420#673420" for advice to add the
-x
option to clean:Note
-x
flag will remove all files ignored by Git, so be careful (see the discussion in the answer I refer to).我认为你可以使用以下命令:
git reset --hard
I think you can use the following command:
git reset --hard
如果您有未提交的更改(仅在工作副本中)并且希望恢复到最新提交中的副本,请执行以下操作:
If you have an uncommitted change (it’s only in your working copy) that you wish to revert to the copy in your latest commit, do the following:
请注意,可能仍然有一些文件看起来不会消失 - 它们可能未经编辑,但由于 CRLF / LF 更改,Git 可能已将它们标记为正在编辑。查看您最近是否对
.gitattributes
进行了一些更改。就我而言,我已将 CRLF 设置添加到 .gitattributes 文件中,因此所有文件都保留在“已修改文件”列表中。更改 .gitattributes 设置使它们消失。
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
.gitattributes
recently.In my case, I've added CRLF settings into the
.gitattributes
file and all the files remained in the "modified files" list because of this. Changing the .gitattributes settings made them disappear.您只需使用以下 Git 命令即可恢复存储库中所做的所有未提交的更改:
示例:
You can just use following Git command which can revert back all the uncommitted changes made in your repository:
Example:
来自 Git 帮助:
From Git help:
我通常使用这种效果很好的方法:
I usually use this way that works well:
安全而漫长的方法:
git Branch todelete
git checkout todelete
git add .
git commit -m "我做了一件坏事,抱歉”
git checkoutdevelopment
gitbranch -D todelete
A safe and long way:
git branch todelete
git checkout todelete
git add .
git commit -m "I did a bad thing, sorry"
git checkout develop
git branch -D todelete
用途:
例如:
Use:
For example: