如何撤消“git commit --amend”而不是“git commit”

天大地大妈咪最大 提交于 2019-12-11 18:08:25

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

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

有没有办法撤消最后一次提交? 如果我执行git reset --hard HEAD^ ,第一次提交也会撤消。

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


#1楼

您始终可以从手册中拆分提交

  • 使用git rebase -i commit ^启动交互式rebase,其中commit是要拆分的提交。 实际上,只要包含该提交,任何提交范围都可以。
  • 使用“编辑”操作标记要拆分的提交。
  • 在编辑提交时,执行git reset HEAD ^。 结果是HEAD被一个重绕,索引也随之而来。 但是,工作树保持不变。
  • 现在将更改添加到您希望在第一次提交中拥有的索引。 您可以使用git add(可能是交互式)或git-gui(或两者)来做到这一点。
  • 使用现在适当的提交消息提交now-current索引。
  • 重复最后两步,直到工作树干净。
  • 用git rebase继续rebase - 继续。

#2楼

使用ref-log

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

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

查看以前索引的完整列表,输入git reflog


#3楼

您需要做的是创建一个新提交,其具有与当前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}

#4楼

  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. 添加文件并将其提交到新提交。


#5楼

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

git log --reflog

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

然后将HEAD重置为任何先前的提交,只需通过以下方式:

git reset SHA1 --hard

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

然后樱桃挑选你需要的其他提交:

git cherry-pick SHA1
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!