用于删除所有更改并恢复到上次提交状态的 git 命令是什么?
我做了很多无用的更改,并且想恢复到我的存储库在进行任何更改之前的状态。
有没有 git 命令可以做到这一点?
谢谢!
I made a lot of unhelpful changes, and would like to revert to the state my repo was in before any of the changes.
Is there a git command for doing that?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,要恢复对跟踪文件的更改:
git reset
单独重置索引;添加--hard
也会重置工作副本。如果您已经提交,请指定要重置为的不同提交 - 例如HEAD^
以恢复到HEAD
的父提交(即删除最新提交) 。接下来,要删除所有未跟踪的文件:
-d
告诉它删除目录,-f
强制它实际执行删除,-x
跳过 .gitignore 文件。First, to revert changes to tracked files:
git reset
alone resets the index; adding--hard
resets the working copy as well. If you've already committed, specify a different commit to reset to - eg,HEAD^
to revert to the parent commit ofHEAD
(ie, to remove the latest commit).Next, to delete all untracked files:
-d
tells it to delete directories,-f
forces it to actually do the delete, and-x
skips.gitignore
d files.如果我理解正确,您已经提交了一些内容并希望将其恢复
git reset --hard HEAD^
如果您没有提交任何内容,而只是您的工作树被搞乱了,那么
git reset - -硬头
If I understand correctly you have committed something and want it reverted
git reset --hard HEAD^
If you haven't committed anything and it's only your working tree that is messed up then
git reset --hard HEAD