首页 文章

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

提问于
浏览
1016

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

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

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

9 回答

  • 1

    差不多9年了,但并没有像其中的一些组合,类似于最佳答案(https://stackoverflow.com/a/1459264/4642530) .

    Search all detached heads on branch

    git reflog show origin/BRANCH_NAME --date=relative

    然后找到SHA1哈希

    Reset to old SHA1

    git reset --hard SHA1

    Then push it back up.

    git push origin BRANCH_NAME

    Done.

    这将使您完全恢复到旧提交 .

    (包括先前覆盖的分离提交头的日期)

  • 3
    • 使用上次提交结帐到临时分支

    git branch temp HEAD@{1}

    • 重置上次提交

    git reset temp

    • 现在,您将拥有提交的所有文件以及之前的提交 . 检查所有文件的状态 .

    git status

    • 从git阶段重置提交文件 .

    git reset myfile1.js (等)

    • 重新附加此提交

    git commit -C HEAD@{1}

    • 添加文件并将其提交到新提交 .
  • 13

    可能值得注意的是,如果您仍然在编辑器中使用提交消息,则可以删除提交消息,它将中止 git commit --amend 命令 .

  • 112

    你可以在下面撤消你的“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”,推送到分支

  • 12

    使用ref-log

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

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

    查看以前索引的完整列表类型 git reflog

  • 2

    您始终可以拆分提交,来自manual

    • 使用git rebase -i commit ^启动交互式rebase,其中commit是要拆分的提交 . 实际上,任何提交范围都可以,只要它包含该提交 .

    • 使用操作"edit"标记要分割的提交 .

    • 在编辑提交时,执行git reset HEAD ^ . 结果是HEAD被一个回卷,索引也随之而来 . 但是,工作树保持不变 .

    • 现在将更改添加到第一次提交中要包含的索引 . 您可以使用git add(可能是交互式)或git-gui(或两者)来做到这一点 .

    • 使用现在适当的提交消息提交now-current索引 .

    • 重复最后两步,直到工作树干净 .

    • 使用git rebase继续使用rebase --continue .

  • 38

    也许可以使用 git reflog 在修改之前和修改之后获得两次提交 .

    然后使用 git diff before_commit_id after_commit_id > d.diff 在修改之前和修改之后得到差异 .

    接下来使用 git checkout before_commit_id 在提交之前返回

    最后使用 git apply d.diff 来应用你所做的真正改变 .

    这解决了我的问题 .

  • 21

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

    git log --reflog
    

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

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

    git reset SHA1 --hard
    

    注意:将SHA1替换为您的实际提交哈希 . 另请注意,此命令将丢失任何未提交的更改,因此您可以在之前存储它们 . 或者使用--soft代替 .

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

    git cherry-pick SHA1
    
  • 1781

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

相关问题