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

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

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


当前回答

也许可以使用git-relog在修改前和修改后获得两个提交。

然后使用git diff before_commit_id after_commit_id>d.diff获取修正前和修正后的差异。

接下来使用git checkout before_commit_id返回提交前

最后使用git-apply.diff应用您所做的真正更改。

这解决了我的问题。

其他回答

使用ref日志:

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

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

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

您需要做的是创建一个新的提交,其详细信息与当前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}

简单解决方案解决方案可行:如果您的HEAD提交与远程提交同步。

在本地工作区中再创建一个分支,并使其与远程分支保持同步。Cherry从新创建的分支上执行的分支(gitcommit--modify)中选择HEAD提交。

精心挑选的提交将只包含最新的更改,而不包含旧的更改。您现在可以重命名此提交。

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

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

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

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

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

也许可以使用git-relog在修改前和修改后获得两个提交。

然后使用git diff before_commit_id after_commit_id>d.diff获取修正前和修正后的差异。

接下来使用git checkout before_commit_id返回提交前

最后使用git-apply.diff应用您所做的真正更改。

这解决了我的问题。