[英]Git: How to Undo commit *and* revert to last branch
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).呃哦......我错误地进行了一个非常复杂的更改(包括子目录和文件重命名),但我并不知道我在做什么(或者 Git 会做什么)。
I now want to undo everything such that:我现在想撤消所有这些:
.git
is) to a certain branch (last one will do for now).将当前工作目录( .git
所在的位置)恢复到某个分支(现在最后一个)。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.我找到了对git reset --soft和git reset --hard的引用,但我已经向自己证明,在不完全理解命令的情况下过早使用命令会造成真正的损害。 :) :)
I found the git reset man page but I am still confused as to:我找到了git 重置手册页,但我仍然对以下内容感到困惑:
HEAD
?什么是HEAD
?HEAD
and * master
? HEAD
和* master
有什么区别?--soft
, --hard
or other (3 more options)?在我的情况下(见上文),我是否需要使用--soft
、 --hard
或其他(另外 3 个选项)?git reset
) to "finalize" the reversal?我是否需要运行另一个命令(在执行git reset
之后)来“完成”反转?UPDATE: After reading the answer below:更新:阅读以下答案后:
git reset --hard HEAD^
?我是否正确理解在我的情况下我需要做的就是发出一个命令git reset --hard HEAD^
?HEAD
is the latest commit of the checked-out branch. HEAD
是签出分支的最新提交。master
is a branch (the main branch, by convention) whereas HEAD
is a location in history for the checked-out branch. master
是一个分支(按照惯例,主分支),而HEAD
是签出分支的历史位置。 HEAD
is relative to the branch you are on. HEAD
与您所在的分支相关。git reset --soft
will leave your changes in the working tree, uncommitted for you to do whatever you like with. git reset --soft
会将您的更改留在工作树中,不承诺您可以随心所欲地做任何事情。 git reset --hard
will restore the working tree to the state it was in at the commit you reset to. git reset --hard
会将工作树恢复到您重置到的提交时所在的 state。First, to keep the commit in case you want to inspect it later, make a branch:首先,要保留提交以防您以后要检查它,请创建一个分支:
git checkout -b my_bad_commit
(or alternatively do git branch my_bad_commit
as mentioned in larsman's comment.) (或者做git branch my_bad_commit
,如 larsman 的评论中所述。)
Then return to master
or whatever branch you were on and reset:然后返回到master
或您所在的任何分支并重置:
git checkout branch_with_bad_commit
git reset --hard HEAD^
HEAD^ translates to "the parent of HEAD," which you can even stack for HEAD^^ = 2 commits back. HEAD^ 转换为“HEAD 的父级”,您甚至可以将其堆叠为 HEAD^^ = 2 次提交。 For more on this topic, check the git community book chapter onundo in git有关此主题的更多信息,请查看 git 中有关撤消的 git 社区书籍章节
HEAD
is the tip of the current branch . HEAD
是当前分支的尖端。HEAD
and master
is that HEAD
changes when you checkout a branch (or commit). HEAD
和master
之间的区别在于,当您签出分支(或提交)时HEAD
会发生变化。--soft
will leave the changes around, so you can re-add/commit them or undo them by doing git checkout
on the changed files. --soft
将保留更改,因此您可以通过对更改的文件执行git checkout
出来重新添加/提交或撤消它们。 --hard
will reset the working area to the state of the commit you are resetting to. --hard
会将工作区域重置为您要重置的提交的 state。reset --hard
.如果您reset --hard
则不会。 You might have to git push --force
to remote repos (although, if the changes you made are already on a remote, rewriting history is strongly discouraged).您可能需要git push --force
到远程存储库(尽管,如果您所做的更改已经在远程,则强烈建议不要重写历史记录)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.