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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2845731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:24:04  来源:igfitidea点击:

How to uncommit my last commit in Git

gitundogit-resetgit-index

提问by richard

How can I uncommit my last commit in git?

如何取消提交我在 git 中的最后一次提交?

Is it

是吗

git reset --hard HEAD

or

或者

git reset --hard HEAD^

?

?

回答by Cascabel

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".

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

If you're trying to understand git resetbetter, please see "Can you explain what "git reset" does in plain English?".

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



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,它仍然取决于您所说的“取消提交”是什么意思。如果您只想撤消提交的行为,保持其他所有内容完好无损,请使用:

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 completelyundo 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. HEADrefers to the current commit - generally, the tip of the currently checked-out branch. The ^is a notation which can be attached to anycommit 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.

原来的问题也问它HEAD^不是HEADHEAD指的是当前提交——通常是当前签出分支的提示。的^是可以附连的符号的任何提交说明符,以及装置“的之前提交”。所以,HEAD^是当前提交之前的提交,就像master^主分支尖端之前的提交一样。

Here's the portion of the git-rev-parse documentationdescribing all of the ways to specify commits (^is just a basic one among many).

这是git-rev-parse 文档的一部分,描述了指定提交的所有方法(^只是众多方法中的一个基本方法)。

回答by nfm

git reset --soft HEAD^Will keep the modified changes in your working tree.

git reset --soft HEAD^将在您的工作树中保留修改后的更改。

git reset --hard HEAD^WILL THROW AWAY THE CHANGES YOU MADE !!!

git reset --hard HEAD^将抛弃您所做的更改!!!

回答by Alex K

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.

返回 2 次提交。

Edit: As charsi mentioned, if you are on Windows you will need to put HEAD or commit hash in quotes.

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

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

回答by tgeros

Be careful, reset --hardwill remove your local (uncommitted) modifications, too.

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

git reset --hard HEAD^

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

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

git reset --hard "HEAD^"

回答by dax

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

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

zsh: no matches found: HEAD^

You need to escape the ^

你需要逃离 ^

git reset --soft HEAD\^

回答by splendidthoughts

If you commit to the wrong branch

如果你提交到错误的分支

While on the wrong branch:

在错误的分支上时:

  1. git log -2gives you hashes of 2 last commits, let's say $prevand $last
  2. git checkout $prevcheckout correct commit
  3. git checkout -b new-feature-branchcreates a new branch for the feature
  4. git cherry-pick $lastpatches a branch with your changes
  1. git log -2给你 2 个最后提交的哈希值,让我们说$prev$last
  2. git checkout $prev结帐正确提交
  3. git checkout -b new-feature-branch为该功能创建一个新分支
  4. git cherry-pick $last使用您的更改修补分支

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

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

回答by Allen Kenney

If you haven't pushed your changes yet use git reset --soft [Hash for one commit]to rollback to a specific commit. --softtells git to keep the changes being rolled back (i.e., mark the files as modified). --hardtells git to delete the changes being rolled back.

如果您尚未推送更改,请使用git reset --soft [Hash for one commit]回滚到特定提交。--soft告诉 git 保持正在回滚的更改(即,将文件标记为已修改)。--hard告诉 git 删除正在回滚的更改。

回答by Filipe

Be careful with that.

小心点。

But you can use the rebase command

但是你可以使用 rebase 命令

git rebase -i HEAD~2

A viwill 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.

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