我不小心修改了我之前的提交。提交应该是分开的,以保留我对特定文件所做更改的历史记录。
有没有办法撤销最后一次提交?如果我执行类似 git reset --hard HEAD^
的操作,第一次提交也会被撤消。
(我还没有推送到任何远程目录)
最佳答案
您需要做的是创建一个新提交,其详细信息与当前 HEAD
提交相同,但父级为 HEAD
的先前版本。 git reset --soft
将移动分支指针,以便下一次提交发生在与当前分支头所在位置不同的提交之上。
# 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.)
# The -C option takes the given commit and reuses the log message and
# authorship information.
git commit -C HEAD@{1}
关于git - 如何撤消 "git commit --amend"完成而不是 "git commit",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1459150/