git - 如何撤消 "git commit --amend"完成而不是 "git commit"

标签 git commit undo git-amend

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

有没有办法撤销最后一次提交?如果我执行类似 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 - 如何撤消 "git commit --amend"完成而不是 "git commit",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1459150/

相关文章:

默认情况下,GIT 忽略对已提交文件的本地更改

git - 在 git 中暂存暂存文件?

ios - 如何撤消线条描边 ios

javascript - 如何使用 Word 的 Javascript API 清除 Word 撤消堆栈

git - 如何备份 git stash 内容?

Git - 从之前的提交中 checkout 一个文件,我是否丢失了我拥有的未提交的工作?

commit - 克隆和提交 BitBucket 项目的 "Wiki"存储库的最佳实践

java - Wicket 7.6.0 中的 wicket.version.undo 包

WordPress作为git子模块,不小心通过WP admin更新而不是git,现在修改了内容

bash - svn 从 bash 脚本提交而不打开编辑器