小编典典

如何撤消“git commit --amend”而不是“git commit”

git

我不小心修改了我之前的提交。提交应该是分开的,以保留我对特定文件所做更改的历史记录。

有没有办法撤消最后一次提交?如果我做类似的事情git reset --hard HEAD^,第一次提交也会被撤消。

(我还没有推送到任何远程目录)


阅读 149

收藏
2022-02-21

共1个答案

小编典典

您需要做的是创建一个新的提交,其细节与当前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.)
git commit -C HEAD@{1}
2022-02-21