如何撤消“git commit --amend”完成而不是“git commit”
我不小心修改了之前的提交。提交应该是单独的,以保留我对特定文件所做的更改的历史记录。
有没有办法撤消最后一次提交?如果我执行诸如 git reset --hard HEAD^ 之类的操作,则第一次提交也会被撤消。
(我还没有推送到任何远程目录)
I accidentally amended my previous commit. The commit should have been separate to keep history of the changes I made to a particular file.
Is there a way to undo that last commit? If I do something like git reset --hard HEAD^
, the first commit also is undone.
(I have not yet pushed to any remote directories)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您需要做的是创建一个新提交,其详细信息与当前
HEAD
提交相同,但父级为HEAD
的先前版本。 git reset --soft 将移动分支指针,以便下一次提交发生在与当前分支头现在所在位置不同的提交之上。What you need to do is to create a new commit with the same details as the current
HEAD
commit, but with the parent as the previous version ofHEAD
.git reset --soft
will move the branch pointer so that the next commit happens on top of a different commit from where the current branch head is now.使用
HEAD@{1}
的这些答案都不适合我,所以这是我的解决方案:git reflog
git reset --soft c296452
您的暂存环境现在将包含您意外与 c296452 提交合并的所有更改。
None of these answers with the use of
HEAD@{1}
worked out for me, so here's my solution:git reflog
git reset --soft c296452
Your staging environment will now contain all of the changes that you accidentally merged with the c296452 commit.
使用 ref-log:
然后您应该拥有以前的所有内容修改工作副本中的更改,并可以将它们提交到新的提交中。
要查看以前的头提交的完整列表,请输入
git reflog
。如果您正在寻找单个命令来实现此目的并且不害怕低级命令,您可能会发现我的其他答案很有帮助管道命令。
Use the ref-log:
You should then have all your previously amended changes in your working copy and can commit them to a new commit.
To see a full list of previous head commits type
git reflog
.You might find my other answer helpful if you are looking for a single command to achieve this and are not afraid of low-level plumbing commands.
通过以下方式查找修改后的提交:
注意:为了清晰起见,您可以添加
--patch
来查看提交正文。与 git reflog 相同。然后将您的 HEAD 重置为之前的任何提交:
注意:用您的真实提交替换 SHA1哈希。另请注意,此命令将丢失任何未提交的更改,因此您可以先将它们存储起来。或者,使用
--soft
来保留最新更改,然后提交它们。然后挑选您需要的其他提交:
Find your amended commits by:
Note: You may add
--patch
to see the body of the commits for clarity. Same asgit reflog
.then reset your HEAD to any previous commit at the point it was fine by:
Note: Replace SHA1 with your real commit hash. Also note that this command will lose any uncommitted changes, so you may stash them before. Alternatively, use
--soft
instead to retain the latest changes and then commit them.Then cherry-pick the other commit that you need on top of it:
如果您已将提交推送到远程,然后错误地修改了该提交的更改,这将解决您的问题。发出 git log 来查找提交前的 SHA。 (假设远程名称为 origin)。现在使用该 SHA 发出这些命令。
If you have pushed the commit to remote and then erroneously amended changes to that commit this will fix your problem. Issue a
git log
to find the SHA before the commit. (this assumes remote is named origin). Now issue these command using that SHA.可能值得注意的是,如果您仍在编辑器中显示提交消息,则可以删除提交消息,它将中止 git commit --amend 命令。
Possibly worth noting that if you're still in your editor with the commit message, you can delete the commit message and it will abort the
git commit --amend
command.您始终可以拆分提交,
从 手册
You can always split a commit,
From the manual
也许可以使用 git reflog 来获取修改前和修改后的两次提交。
然后使用 git diff before_commit_id after_commit_id > d.diff 获取修改前和修改后之间的差异。
接下来使用 git checkout before_commit_id 回到提交之前
,最后使用 git apply d.diff 来应用您所做的真正更改。
这解决了我的问题。
Maybe can use
git reflog
to get two commit before amend and after amend.Then use
git diff before_commit_id after_commit_id > d.diff
to get diff between before amend and after amend.Next use
git checkout before_commit_id
to back to before commitAnd last use
git apply d.diff
to apply the real change you did.That solves my problem.
您可以执行以下操作来撤消您的
git commit —amend
git reset --soft HEAD^
git checkout files_from_old_commit_on_branch
git pull origin your_branch_name< /code>
======================================
现在您的更改与之前相同。这样您就完成了 git commit —amend 的撤消操作,
现在您可以执行 git push origin来推送到分支。
You can do below to undo your
git commit —amend
git reset --soft HEAD^
git checkout files_from_old_commit_on_branch
git pull origin your_branch_name
====================================
Now your changes are as per previous. So you are done with the undo for
git commit —amend
Now you can do
git push origin <your_branch_name>
, to push to the branch.几乎晚了 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
完成。
这会将您完全恢复到旧的提交
(包括之前覆盖的分离提交头的日期) 。
Almost 9 years late to this but didn't see this variation mentioned accomplishing the same thing (it's kind of a combination of a few of these, similar to to top answer (https://stackoverflow.com/a/1459264/4642530).
Search all detached heads on branch
git reflog show origin/BRANCH_NAME --date=relative
Then find the SHA1 hash
Reset to old SHA1
git reset --hard SHA1
Then push it back up.
git push origin BRANCH_NAME
Done.
This will revert you back to the old commit entirely.
(Including the date of the prior overwritten detached commit head)
检出到上次提交的临时分支
gitbranch temp HEAD@{1}
重置上次提交
git reset temp
现在,您将拥有您提交以及之前提交的所有文件。检查所有文件的状态。
git status
从 git 阶段重置您的提交文件。
git reset myfile1.js
(依此类推)重新附加此提交
git commit -C HEAD@{1}
添加并提交您的文件到新的提交。
Checkout to temporary branch with last commit
git branch temp HEAD@{1}
Reset last commit
git reset temp
Now, you'll have all files your commit as well as previous commit. Check status of all the files.
git status
Reset your commit files from git stage.
git reset myfile1.js
(so on)Reattach this commit
git commit -C HEAD@{1}
Add and commit your files to new commit.
简单的解决方案
解决方案有效:如果您的 HEAD 提交与远程提交同步。
精心挑选的提交将仅包含您的最新更改,而不包含旧更改。
您现在可以重命名此提交。
Simple Solution
Solution Works Given: If your HEAD commit is in sync with remote commit.
The cherry-picked commit will only contain your latest changes, not the old changes.
You can now just rename this commit.
您可以通过以下方式轻松做到这一点:
git reset --soft HEAD^
git commit -m "new commit msg"
现在推送更改并重新调整此提交您修改更改的最后一次提交的顶部。
这将确保新提交仅修改了更改。
This is how you can do it easily:
git reset --soft HEAD^
git commit -m "new commit msg"
Now push the changes and rebase this commit on top of last commit on which you had amended your changes.
This will make sure that the new commit has only amended changes.
您可以使用低级
commit-tree
命令和 reflog< /a> 使用当前的头提交树构造一个新的提交,但将原始(修改前)提交作为父提交。通过重置工作树和索引来转发分支:为什么会这样?让我们看一些 ASCII 绘图:
这是我们开始的地方,但是修改提交后,我们剩下:
提交 C 不再可以从分支访问,但它仍然存在于 Git 的数据库中,并在引用日志中列出为
HEAD@{1}
。您想要的是使用修改后的提交树创建以下新提交:
并且
git commit-tree HEAD^{tree} -p HEAD@{1}
正是这样做的:它创建提交 D与树 X(与修改提交 C' 相同的树),但将父级设置为 C(修改之前的提交)。需要使用 git reset 将分支从错误的、修改后的提交移动到正确的、单独的提交。You can use the low-level
commit-tree
command and the reflog to construct a new commit with your current head commit's tree, but the original (pre-amend) commit as parent. The branch is forwarded by resetting both working tree and index:Why does this work? Let's have a look at some ASCII drawings:
This is where we start, but after amending the commit, we are left with:
Commit C is no longer reachable from the branch, but it still exists in Git's database and is listed in the reflog as
HEAD@{1}
.What you want is to create the following new commit with the tree of the amended commit:
and
git commit-tree HEAD^{tree} -p HEAD@{1}
does exactly that: it creates commit D with tree X (same tree as of amended commit C'), but sets the parent to C (the commit before being amended).git reset
is required to move the branch from the erroneous, amended commit to the correct, separate commit.