如何取消 Git 中的最后一次提交
如何取消 git 中的最后一次提交?
是
git reset --hard HEAD
还是
git reset --hard HEAD^
?
How can I uncommit my last commit in git?
Is it
git reset --hard HEAD
or
git reset --hard HEAD^
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您不完全确定“取消提交”的含义,并且不知道是否要使用
git reset
,请参阅“恢复到之前的 Git 提交"。如果您想更好地理解
git reset
,请参阅“你能用简单的英语解释一下“git reset”的作用吗?”。如果您知道要使用 git reset,它仍然取决于您所说的“取消提交”的含义。如果您想要做的只是撤消提交行为,使其他所有内容保持不变,请使用:
如果您想撤消提交行为和您暂存的所有内容,但保持工作树(您的文件)完好无损:
如果您实际上想要完全撤消它,丢弃所有未提交的更改,将所有内容重置为之前的提交(如原始问题所问):
原始问题还问它是
HEAD^
而不是HEAD
。HEAD
指的是当前提交 - 通常是当前签出分支的提示。^
是一个可以附加到任何提交说明符的符号,意思是“之前的提交”。因此,HEAD^
是当前分支之前的提交,就像master^
是 master 分支尖端之前的提交一样。这是 git-rev-parse 文档 的部分,描述了所有指定提交的方法(
^
只是众多方法中的一种基本方法)。If you aren't totally sure what you mean by "uncommit" and don't know if you want to use
git reset
, please see "Revert to a previous Git commit".If you're trying to understand
git reset
better, please see "Can you explain what "git reset" does in plain English?".If you know you want to use
git reset
, it still depends what you mean by "uncommit". If all you want to do is undo the act of committing, leaving everything else intact, use:If you want to undo the act of committing and everything you'd staged, but leave the work tree (your files) intact:
And if you actually want to completely undo it, throwing away all uncommitted changes, resetting everything to the previous commit (as the original question asked):
The original question also asked it's
HEAD^
notHEAD
.HEAD
refers to the current commit - generally, the tip of the currently checked-out branch. The^
is a notation which can be attached to any commit specifier, and means "the commit before". So,HEAD^
is the commit before the current one, just asmaster^
is the commit before the tip of the master branch.Here's the portion of the git-rev-parse documentation describing all of the ways to specify commits (
^
is just a basic one among many).git reset --soft HEAD^
将在您的工作树中保留修改后的更改。git reset --hard HEAD^
将丢弃您所做的更改!!!git reset --soft HEAD^
Will keep the modified changes in your working tree.git reset --hard HEAD^
WILL THROW AWAY THE CHANGES YOU MADE !!!保留要撤消的提交中的更改 销毁
要撤消的提交中的更改
您也可以说
返回 2 个提交。
编辑:正如 charsi 提到的,如果您使用的是 Windows,则需要将 HEAD 或提交哈希放在引号中。
To keep the changes from the commit you want to undo
To destroy the changes from the commit you want to undo
You can also say
to go back 2 commits.
Edit: As charsi mentioned, if you are on Windows you will need to put HEAD or commit hash in quotes.
如果您想恢复提交而不丢弃工作,请使用 --soft 标志而不是 --hard
小心!
reset --hard
也会删除您的本地(未提交)修改。注意:如果你在 Windows 上,你需要引用 HEAD^ 所以
If you want to revert the commit WITHOUT throwing away work, use the --soft flag instead of --hard
Be careful !
reset --hard
will remove your local (uncommitted) modifications, too.note: if you're on windows you'll need to quote the HEAD^ so
请注意 - 如果您使用 ZSH 并看到错误
You need to escape the
^
Just a note - if you're using ZSH and see the error
You need to escape the
^
如果您提交到错误的分支
在错误的分支上时:
git log -2
为您提供 2 个最后提交的哈希值,假设$prev
和$last
git checkout $prev
checkout 正确提交git checkout -b new-feature-branch
为该功能创建一个新分支gitcherry-pick $last
用您的更改修补分支然后您可以按照上面建议的方法之一从第一个分支中删除您的提交。
If you commit to the wrong branch
While on the wrong branch:
git log -2
gives you hashes of 2 last commits, let's say$prev
and$last
git checkout $prev
checkout correct commitgit checkout -b new-feature-branch
creates a new branch for the featuregit cherry-pick $last
patches a branch with your changesThen you can follow one of the methods suggested above to remove your commit from the first branch.
如果您尚未推送更改,请使用 git reset --soft [一次提交的哈希值] 回滚到特定提交。
--soft
告诉 git 保持回滚更改(即,将文件标记为已修改)。--hard
告诉 git 删除正在回滚的更改。If you haven't pushed your changes yet use
git reset --soft [Hash for one commit]
to rollback to a specific commit.--soft
tells git to keep the changes being rolled back (i.e., mark the files as modified).--hard
tells git to delete the changes being rolled back.对此要小心。
但是您可以使用 rebase 命令,
vi
将打开,您所要做的就是删除包含提交的行。还可以阅读正确版本 @vi
中显示的说明。在此模式下可以执行一些操作。Be careful with that.
But you can use the rebase command
A
vi
will open and all you have to do is delete the line with the commit. Also can read instructions that were shown in proper edition @vi
. A couple of things can be performed on this mode.