我无意中修改了之前的承诺。提交应该是单独的,以保存我对特定文件所做的更改的历史记录。

有没有办法撤销最后的承诺?如果我执行类似gitreset-hardHEAD^的操作,第一次提交也会被撤销。

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


当前回答

可能值得注意的是,如果您仍然在编辑器中看到提交消息,您可以删除提交消息,它将中止gitcommit--modify命令。

其他回答

您需要做的是创建一个新的提交,其详细信息与当前HEAD提交相同,但父级与先前版本的HEAD相同。gitreset-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}

可能值得注意的是,如果您仍然在编辑器中看到提交消息,您可以删除提交消息,它将中止gitcommit--modify命令。

使用HEAD@{1}的这些答案对我来说都不可行,所以下面是我的解决方案:

数字刷新

d0c9f22 HEAD@{0}: commit (amend): [Feature] - ABC Commit Description 
c296452 HEAD@{1}: commit: [Feature] - ABC Commit Description 

git重置--软c296452

您的登台环境现在将包含您意外与c296452提交合并的所有更改。

您可以执行以下操作来撤消git提交-修改

git reset—软头^git签出文件_from_old_commit_onbranchgit拉动原点your_branch_name

====================================

现在,您的更改与之前相同。所以你已经完成了gitcommit的撤销-修改

现在您可以执行gitpushorigin<your_branch_name>,以推送到分支。

使用ref日志:

git branch fixing-things HEAD@{1}
git reset fixing-things

然后,您应该只在工作副本中保存所有以前修改过的更改,然后可以再次提交

要查看以前索引的完整列表,请键入git-relog