1649

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

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

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

4

12 回答 12

2910

您需要做的是创建一个新的提交,其细节与当前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}
于 2009-09-22T10:23:44.350 回答
170

使用参考日志

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

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

查看以前索引类型的完整列表git reflog

于 2009-09-22T10:02:55.730 回答
120

通过以下方式查找修改后的提交:

git log --reflog

注意:为了清楚起见,您可以添加--patch以查看提交的正文。与 相同git reflog

然后通过以下方式将您的 HEAD 重置为之前的任何提交:

git reset SHA1 --hard

注意:SHA1 替换为您的真实提交哈希。另请注意,此命令将丢失任何未提交的更改,因此您可以先将它们存储起来。或者,改为使用--soft保留最新更改,然后提交它们。

然后挑选你需要的另一个提交:

git cherry-pick SHA1
于 2016-03-27T01:56:18.807 回答
74

使用 的这些答案都没有HEAD@{1}为我解决,所以这是我的解决方案:

git reflog

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

git reset --soft c296452

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

于 2021-01-30T19:49:55.723 回答
30

如果您已将提交推送到远程,然后错误地修改了对该提交的更改,这将解决您的问题。git log在提交之前发出 a以查找 SHA。(这假设远程被命名为原点)。现在使用该 SHA 发出这些命令。

git reset --soft <SHA BEFORE THE AMMEND>
#you now see all the changes in the commit and the amend undone

#save ALL the changes to the stash
git stash

git pull origin <your-branch> --ff-only
#if you issue git log you can see that you have the commit you didn't want to amend

git stash pop
#git status reveals only the changes you incorrectly amended

#now you can create your new unamended commit
于 2019-04-24T18:26:25.177 回答
25

您可以随时拆分提交,来自手册

  • 使用 git rebase -i commit^ 启动交互式 rebase,其中 commit 是您要拆分的提交。事实上,任何提交范围都可以,只要它包含该提交。
  • 使用“编辑”操作标记要拆分的提交。
  • 在编辑该提交时,执行 git reset HEAD^。效果是 HEAD 倒带一,索引也随之而来。但是,工作树保持不变。
  • 现在将更改添加到您希望在第一次提交中拥有的索引。您可以使用 git add (可能以交互方式)或 git-gui (或两者)来做到这一点。
  • 使用现在合适的任何提交消息提交当前索引。
  • 重复最后两个步骤,直到您的工作树干净为止。
  • 使用 git rebase --continue 继续 rebase。
于 2009-09-22T10:01:22.310 回答
24

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

于 2017-03-03T18:45:09.960 回答
20

也许可以用来git reflog在修改前和修改后获得两次提交。

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

下次使用git checkout before_commit_id返回到提交之前

最后用于git apply d.diff应用​​您所做的真正更改。

这解决了我的问题。

于 2016-09-20T12:39:49.167 回答
10

您可以执行以下操作来撤消您的git commit —amend

  1. git reset --soft HEAD^
  2. git checkout files_from_old_commit_on_branch
  3. git pull origin your_branch_name

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

现在您的更改与以前一样。所以你完成了撤消git commit —amend

现在你可以做git push origin <your_branch_name>,推送到分支。

于 2018-06-25T11:46:23.123 回答
8

晚了将近 9 年,但没有看到提到的这种变化完成了同样的事情(它是其中一些的组合,类似于最佳答案(https://stackoverflow.com/a/1459264/4642530) .

搜索分支上的所有分离头

git reflog show origin/BRANCH_NAME --date=relative

然后找到SHA1哈希

重置为旧的 SHA1

git reset --hard SHA1

然后把它推回去。

git push origin BRANCH_NAME

完毕。

这将使您完全恢复到旧的提交。

(包括之前覆盖的分离提交头的日期)

于 2018-06-17T21:55:20.993 回答
3
  1. 使用最后一次提交签出到临时分支

    git branch temp HEAD@{1}

  2. 重置上次提交

    git reset temp

  3. 现在,您将拥有您提交的所有文件以及之前的提交。检查所有文件的状态。

    git status

  4. 从 git 阶段重置您的提交文件。

    git reset myfile1.js(很快)

  5. 重新附加此提交

    git commit -C HEAD@{1}

  6. 添加文件并将其提交到新的提交。

于 2016-03-04T07:28:58.390 回答
3

给定的简单解决 方案解决方案有效:如果您的 HEAD 提交与远程提交同步。

  • 在您的本地工作空间中再创建一个分支,并使其与您的远程分支保持同步。
  • Cherry 从分支中挑选 HEAD 提交(其中 git commit --amend)被执行到新创建的分支上。

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

于 2020-09-10T13:47:39.440 回答