Git:如何撤消提交*和*恢复到上一个分支
呃哦...我错误地犯了一个相当复杂的更改(包括子目录和文件重命名),而没有真正知道我在做什么(或者 Git 会做什么)。
我现在想撤消一切,例如:
- 提交完全相反(就好像 也许从来没有做过 也将其从历史记录中删除)
- 恢复当前工作目录 (其中
.git
是)到某个 分支(现在用最后一个分支即可)。
我找到了对 git reset --soft 和 git但是我已经向自己证明,在不完全理解命令的情况下过早使用命令会造成真正的损害。 :)
我找到了 git Reset 手册页 但我仍然很困惑:
- 什么是
HEAD
吗? - 有什么区别
HEAD
和* master
? - 在我的情况下(见上文)我是否 需要使用
--soft
、--hard
或 其他(还有 3 个选项)? - 我需要运行另一个命令吗 (在执行
git重置
之后) “最终”逆转?
更新:阅读以下答案后:
- 我是否正确理解了我所做的一切 在我的情况下需要做的是问题 单个命令 git reset --hard 头^?
- 我如何验证逆转已发生 执行正确吗?
Uh oh... I mistakenly committed a pretty complex change (including subdirectory and files renames) without really knowing what I am doing (or what Git would be doing).
I now want to undo everything such that:
- commit is completely reversed (as if
it has never been done, perhaps
removing it from history as well) - Restore current working directory
(where.git
is) to a certain
branch (last one will do for now).
I found references to git reset --soft and git reset --hard but I have already proven to myself that I can do real damage by prematurely using a command without fully understanding it. :)
I found the git reset man page but I am still confused as to:
- What is
HEAD
? - What is the difference between
HEAD
and* master
? - In my situation (see above) do I
need to use--soft
,--hard
or
other (3 more options)? - Do I need to run another command
(after doinggit reset
) to
"finalize" the reversal?
UPDATE: After reading the answer below:
- Do I understand correctly that all I
need to do in my situation is issue
a single commandgit reset --hard
?
HEAD^ - How do I verify that reversal was
performed correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HEAD
是签出分支的最新提交。master
是一个分支(按照惯例,主分支),而HEAD
是签出分支的历史位置。HEAD
与您所在的分支相关。首先,为了保留提交以供稍后检查,请创建一个分支:(
或者按照 larsman 的评论中提到的那样执行
gitbranch my_bad_commit
。)然后返回到
master
或您所在的任何分支并重置:HEAD^ 翻译为“HEAD 的父级”,您甚至可以堆叠 HEAD^^ = 2 次提交。有关此主题的更多信息,请查看 git 社区书籍中关于git 中的撤消的章节
HEAD
is the latest commit of the checked-out branch.master
is a branch (the main branch, by convention) whereasHEAD
is a location in history for the checked-out branch.HEAD
is relative to the branch you are on.git reset --soft
will leave your changes in the working tree, uncommitted for you to do whatever you like with.git reset --hard
will restore the working tree to the state it was in at the commit you reset to.First, to keep the commit in case you want to inspect it later, make a branch:
(or alternatively do
git branch my_bad_commit
as mentioned in larsman's comment.)Then return to
master
or whatever branch you were on and reset:HEAD^ translates to "the parent of HEAD," which you can even stack for HEAD^^ = 2 commits back. For more on this topic, check the git community book chapter on undo in git
HEAD
是当前分支的提示。HEAD
和master
之间的区别在于,HEAD
在您签出分支(或提交)时发生变化。--soft
将保留更改,因此您可以通过对更改的文件执行git checkout
来重新添加/提交它们或撤消它们。--hard
会将工作区域重置为您要重置的提交状态。reset --hard
,则不会。您可能必须 git push --force 到远程存储库(不过,如果您所做的更改已经在远程上,则强烈建议不要重写历史记录)。HEAD
is the tip of the current branch.HEAD
andmaster
is thatHEAD
changes when you checkout a branch (or commit).--soft
will leave the changes around, so you can re-add/commit them or undo them by doinggit checkout
on the changed files.--hard
will reset the working area to the state of the commit you are resetting to.reset --hard
. You might have togit push --force
to remote repos (although, if the changes you made are already on a remote, rewriting history is strongly discouraged).