我不小心修改了之前的提交。这个提交应该是独立的,以便保留我对特定文件所做更改的历史记录。
有没有办法撤销上一个提交?如果我执行类似于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.)
# The -C option takes the given commit and reuses the log message and
# authorship information.
git commit -C HEAD@{1}
git reflog
来查找正确的编号,例如{2}
。 - JJDgit commit --amend
命令之前的 HEAD、工作目录(未更改)和索引状态。第二个命令是将操作重做到一个新的提交中。这些命令适用于任何 git commit
,而不仅仅是 --amend
。 - cdunn2001git commit
。 - Matt Montaggit reset --soft HEAD@{1}
时出现错误:fatal: ambiguous argument 'HEAD@1': unknown revision or path not in the working tree. Use '--' to separate paths from revisions
。当我用在 git reflog
中显示的等效提交哈希替换了 HEAD@{1}
时(感谢JJD!),这个答案非常有效! - Tim ArnoldHEAD@{1}
。例如,在tcsh中运行命令 echo HEAD@{1}
,输出结果会是 HEAD@1
,因为tcsh会解释花括号。如果使用单引号,则花括号将得到保留。 - Kelvingit branch fixing-things HEAD@{1}
git reset --soft fixing-things
git reflog
。HEAD~1
和HEAD^
完全相同,都指代当前提交的_父节点_。而另一方面,HEAD@{1}
指的是在此之前HEAD
所指向的提交,也就是说当你切换到一个不同的分支或修补一个提交时,它们意味着不同的提交。 - knittlgit reset HEAD@{1}
就足够了。 - dwelle我尝试了使用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 commit --amend
,因此其他建议都不起作用了。但这个方法有效。谢谢。 - stackomatikergit reset --soft HEAD@{1}
来撤销最新的修改。 - Noam Manosgit reset --soft "HEAD@{1}"
需要加上引号。{1}
是一种特殊的语法。 - TankorSmash通过以下方式查找您的修改后提交记录:
git log --reflog
注意:您可以添加--patch
以查看提交的主体内容,以便更清晰地了解。与git reflog
相同。
然后通过以下方式将HEAD重置为之前任何一次提交的状态:
git reset SHA1 --hard
注意: 用实际的提交哈希替换 SHA1。还要注意,此命令将丢失任何未提交的更改,因此您可能需要在执行之前将它们存储。或者,改为使用 --soft
以保留最新更改,然后再提交。
然后在其上方cherry-pick其它需要的提交:
git cherry-pick SHA1
git reset SHA1 --soft
,你可以保留最新的更改并将它们提交。 - pravjgit log --reflog -p -- {{name-of-the-dir-or-file-in-question}}
命令。它会显示实际的更改和提交信息。 - Kay Vgit reset SHA1 && git commit -m“abc”
- Meukoreset --soft
的好处(在我看来在这种情况下)是将更改放回索引中(而不是工作树)。这样,如果您再次提交,您将提交与刚刚重置的提交相同的更改。当我错误地修改提交时,这对我很方便。 - Felipe Romerogit reset --soft 'HEAD@{1}'
- Felipe Romerogit log
命令以查找提交之前的SHA。(假设远程命名为origin)。现在使用该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
即可。 - knittlgit reset
而不是git reset --soft
,然后执行git add --patch
即可。 - geekofalltradesgit reflog
才是解决之道。 - corinnaerin或许可以使用 git reflog
命令获取修改之前和修改之后的两个提交记录。
然后,使用 git diff before_commit_id after_commit_id > d.diff
命令获取修改前后的差异,并将其保存在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>
,将更改推送到分支。
git restore --staged <YOUR_FILES>
以取消暂存的更改。 - Ravi Patel虽然晚了近9年,但是没有看到提到这种方法的变化(这是几个方法的组合,类似于顶部答案 (https://dev59.com/hHM_5IYBdhLWcg3wQQpd#1459264))。
搜索分支上的所有分离头
git reflog show origin/BRANCH_NAME --date=relative
然后找到SHA1哈希值
回退到旧的SHA1
git reset --hard SHA1
然后将其推送回去。
git push origin BRANCH_NAME
完成。
这将完全恢复您回到旧的提交状态。
(包括之前被覆盖的分离提交头的日期)
--soft
以保留我的更改。我只想将其单独提交。 - Ruan Mendes
git log --reflog -p -- {{name-of-the-dir-or-file-in-question}}
命令。它会显示每个操作的实际更改和提交消息。 - Kay V