如何撤消“git commit --amend”完成而不是“git commit” [英] How to undo "git commit --amend" done instead of "git commit"
问题描述
我意外地修改了我以前的提交。提交应该是分开的,以保留对特定文件所做更改的历史记录。
有没有办法撤消最后一次提交?如果我做了像 git reset --hard HEAD ^
之类的东西,那么第一次提交也会被撤销。
(I尚未推送到任何远程目录)
您需要做的是创建一个新的提交,当前 HEAD
提交,但是父节点与之前版本的 HEAD
不同。 git reset --soft
会移动分支指针,以便下一个提交发生在当前分支头部的不同提交之上。
#移动当前头使其指向旧提交
#保留索引以重做提交。
#HEAD @ {1}为您提供在
#之前HEAD指向的提交,它被移动到它当前指向的位置。请注意,这是
#与HEAD〜1的不同之处,它提供了该提交是HEAD当前指向的提交的
#父节点。
git reset --soft HEAD @ {1}
#使用之前
#HEAD提交的提交详细信息提交当前树。 (请注意,HEAD @ {1}指向的地方与
#previous命令不同,它现在指向错误修改的提交。)
git commit -C HEAD @ {1}
I accidentally amended my previous commit. The commit should have been separate to keep history of the changes I made to a particular file.
Is there a way to undo that last commit? If I do something like git reset --hard HEAD^
, the first commit also is undone.
(I have not yet pushed to any remote directories)
What you need to do is to create a new commit with the same details as the current HEAD
commit, but with the parent as the previous version of HEAD
. git reset --soft
will move the branch pointer so that the next commit happens on top of a different commit from where the current branch head is now.
# Move the current head so that it's pointing at the old commit
# Leave the index intact for redoing the commit.
# HEAD@{1} gives you "the commit that HEAD pointed at before
# it was moved to where it currently points at". Note that this is
# different from HEAD~1, which gives you "the commit that is the
# parent node of the commit that HEAD is currently pointing to."
git reset --soft HEAD@{1}
# commit the current tree using the commit details of the previous
# HEAD commit. (Note that HEAD@{1} is pointing somewhere different from the
# previous command. It's now pointing at the erroneously amended commit.)
git commit -C HEAD@{1}
这篇关于如何撤消“git commit --amend”完成而不是“git commit”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!