我不小心修改了我之前的提交。提交应该是分开的,以保留我对特定文件所做更改的历史记录。
有没有办法撤消最后一次提交?如果我做类似的事情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.)
git commit -C HEAD@{1}
使用参考日志:
git branch fixing-things HEAD@{1}
git reset fixing-things
然后,您应该只在您的工作副本中拥有所有以前修改过的更改,并且可以再次提交
查看以前索引类型的完整列表git reflog
通过以下方式查找修改后的提交:
git log --reflog
注意:为了清楚起见,您可以添加--patch
以查看提交的正文。与 相同git reflog
。
然后通过以下方式将您的 HEAD 重置为之前的任何提交:
git reset SHA1 --hard
注意:将SHA1 替换为您的真实提交哈希。另请注意,此命令将丢失任何未提交的更改,因此您可以先将它们存储起来。或者,改为使用--soft
保留最新更改,然后提交它们。
然后挑选你需要的另一个提交:
git cherry-pick SHA1
使用 的这些答案都没有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 提交合并的所有更改。
如果您已将提交推送到远程,然后错误地修改了对该提交的更改,这将解决您的问题。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
您可以随时拆分提交,来自手册
可能值得注意的是,如果您仍在编辑器中看到提交消息,您可以删除提交消息,它将中止git commit --amend
命令。
也许可以用来git reflog
在修改前和修改后获得两次提交。
然后用于git diff before_commit_id after_commit_id > d.diff
获取修改前和修改后的差异。
下次使用git checkout before_commit_id
返回到提交之前
最后用于git apply d.diff
应用您所做的真正更改。
这解决了我的问题。
您可以执行以下操作来撤消您的git commit —amend
git reset --soft HEAD^
git checkout files_from_old_commit_on_branch
git pull origin your_branch_name
=====================================
现在您的更改与以前一样。所以你完成了撤消git commit —amend
现在你可以做git push origin <your_branch_name>
,推送到分支。
晚了将近 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
完毕。
这将使您完全恢复到旧的提交。
(包括之前覆盖的分离提交头的日期)
使用最后一次提交签出到临时分支
git branch temp HEAD@{1}
重置上次提交
git reset temp
现在,您将拥有您提交的所有文件以及之前的提交。检查所有文件的状态。
git status
从 git 阶段重置您的提交文件。
git reset myfile1.js
(很快)
重新附加此提交
git commit -C HEAD@{1}
添加文件并将其提交到新的提交。
给定的简单解决 方案解决方案有效:如果您的 HEAD 提交与远程提交同步。
精心挑选的提交将仅包含您的最新更改,而不包含旧更改。您现在可以重命名此提交。