如何取消 Git 中的最后一次提交

发布于 2024-09-01 09:58:11 字数 153 浏览 4 评论 0原文

如何取消 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

﹏雨一样淡蓝的深情 2024-09-08 09:58:12

如果您不完全确定“取消提交”的含义,并且不知道是否要使用 git reset,请参阅“恢复到之前的 Git 提交"。

如果您想更好地理解 git reset,请参阅“你能用简单的英语解释一下“git reset”的作用吗?”。


如果您知道要使用 git reset,它仍然取决于您所说的“取消提交”的含义。如果您想要做的只是撤消提交行为,使其他所有内容保持不变,请使用:

git reset --soft HEAD^

如果您想撤消提交行为和您暂存的所有内容,但保持工作树(您的文件)完好无损:

git reset HEAD^

如果您实际上想要完全撤消它,丢弃所有未提交的更改,将所有内容重置为之前的提交(如原始问题所问):

git reset --hard HEAD^

原始问题还问它是 HEAD^ 而不是 HEADHEAD 指的是当前提交 - 通常是当前签出分支的提示。 ^ 是一个可以附加到任何提交说明符的符号,意思是“之前的提交”。因此,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:

git reset --soft HEAD^

If you want to undo the act of committing and everything you'd staged, but leave the work tree (your files) intact:

git reset HEAD^

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):

git reset --hard HEAD^

The original question also asked it's HEAD^ not HEAD. 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 as master^ 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).

无可置疑 2024-09-08 09:58:12

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 !!!

荆棘i 2024-09-08 09:58:12

保留要撤消的提交中的更改 销毁

git reset --soft HEAD^

要撤消的提交中的更改

git reset --hard HEAD^

您也可以说

git reset --soft HEAD~2

返回 2 个提交。

编辑:正如 charsi 提到的,如果您使用的是 Windows,则需要将 HEAD 或提交哈希放在引号中。

git reset --soft "HEAD^"
git reset --soft "asdf"

To keep the changes from the commit you want to undo

git reset --soft HEAD^

To destroy the changes from the commit you want to undo

git reset --hard HEAD^

You can also say

git reset --soft HEAD~2

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.

git reset --soft "HEAD^"
git reset --soft "asdf"
妥活 2024-09-08 09:58:12

如果您想恢复提交而不丢弃工作,请使用 --soft 标志而不是 --hard

git reset --soft HEAD^

小心! reset --hard 也会删除您的本地(未提交)修改。

git reset --hard HEAD^

注意:如果你在 Windows 上,你需要引用 HEAD^ 所以

git reset --hard "HEAD^"

If you want to revert the commit WITHOUT throwing away work, use the --soft flag instead of --hard

git reset --soft HEAD^

Be careful ! reset --hard will remove your local (uncommitted) modifications, too.

git reset --hard HEAD^

note: if you're on windows you'll need to quote the HEAD^ so

git reset --hard "HEAD^"
寻找一个思念的角度 2024-09-08 09:58:12

请注意 - 如果您使用 ZSH 并看到错误

zsh: no matches found: HEAD^

You need to escape the ^

git reset --soft HEAD\^

Just a note - if you're using ZSH and see the error

zsh: no matches found: HEAD^

You need to escape the ^

git reset --soft HEAD\^
很快妥协 2024-09-08 09:58:12

如果您提交到错误的分支

在错误的分支上时:

  1. git log -2 为您提供 2 个最后提交的哈希值,假设 $prev$last
  2. git checkout $prev checkout 正确提交
  3. git checkout -b new-feature-branch 为该功能创建一个新分支
  4. gitcherry-pick $last 用您的更改修补分支

然后您可以按照上面建议的方法之一从第一个分支中删除您的提交。

If you commit to the wrong branch

While on the wrong branch:

  1. git log -2 gives you hashes of 2 last commits, let's say $prev and $last
  2. git checkout $prev checkout correct commit
  3. git checkout -b new-feature-branch creates a new branch for the feature
  4. git cherry-pick $last patches a branch with your changes

Then you can follow one of the methods suggested above to remove your commit from the first branch.

夏日浅笑〃 2024-09-08 09:58:12

如果您尚未推送更改,请使用 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.

翻了热茶 2024-09-08 09:58:12

对此要小心。

但是您可以使用 rebase 命令,

git rebase -i HEAD~2

vi 将打开,您所要做的就是删除包含提交的行。还可以阅读正确版本 @ vi 中显示的说明。在此模式下可以执行一些操作。

Be careful with that.

But you can use the rebase command

git rebase -i HEAD~2

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文