本文翻译自:How to uncommit my last commit in Git [duplicate]
This question already has an answer here: 这个问题已经在这里有了答案:
- How do I undo the most recent local commits in Git? 如何撤消Git中的最新本地提交? 81 answers 81个答案
How can I uncommit my last commit in git? 如何取消对git的最后提交?
Is it 是吗
git reset --hard HEAD
or 要么
git reset --hard HEAD^
? ?
#1楼
参考:https://stackoom.com/question/BwIt/如何取消提交我在Git中的最后一次提交-重复
#2楼
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"
#3楼
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\^
#4楼
Be careful with that. 小心点。
But you can use the rebase command 但是您可以使用rebase命令
git rebase -i HEAD~2
A vi
will open and all you have to do is delete the line with the commit. 将打开一个vi
,您要做的就是删除带有提交的行。 Also can read instructions that were shown in proper edition @ vi
. 也可以阅读在正确版本@ vi
中显示的说明。 A couple of things can be performed on this mode. 在此模式下可以执行几件事。
#5楼
If you haven't pushed your changes yet use git reset --soft [Hash for one commit]
to rollback to a specific commit. 如果尚未推送更改,请使用git reset --soft [Hash for one commit]
回滚到特定的提交。 --soft
tells git to keep the changes being rolled back (ie, mark the files as modified). --soft
告诉git保持回滚更改(即,将文件标记为已修改)。 --hard
tells git to delete the changes being rolled back. --hard
告诉git删除正在回滚的更改。
#6楼
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 log -2
为您提供2次最后提交的哈希值,例如$prev
和$last
-
git checkout $prev
checkout correct commitgit checkout $prev
签出正确的提交 -
git checkout -b new-feature-branch
creates a new branch for the featuregit checkout -b new-feature-branch
branch为功能创建一个新分支 -
git cherry-pick $last
patches a branch with your changesgit cherry-pick $last
用您的更改修补分支
Then you can follow one of the methods suggested above to remove your commit from the first branch. 然后,您可以按照上面建议的方法之一从第一个分支中删除提交。