代码之家  ›  专栏  ›  技术社区  ›  Linda Lawton - DaImTo

如何在不丢失更改的情况下恢复提交?

git
  •  1
  • Linda Lawton - DaImTo  · 技术社区  · 7 年前

    我刚刚意识到,我所做的最后三个承诺是针对主分支的。

    enter image description here

    我知道我可以

    Git reset --hard
    

    Git reset --soft 
    

    但这并不意味着你做了什么。

    我需要将这些更改取消提交,以便将它们移动到自己的分支。我不顾一切地不想失去所有这些变化。如果没有代码检查,我不能推到master,所以我需要将这些更改从master分支转移到它们自己的分支中。

    1 回复  |  直到 7 年前
        1
  •  3
  •   Romain Valeri    6 年前

    你的 git reset --soft 是正确的方法,但是您也必须指向正确的提交,并且在重置之后您还没有完全完成。

    # reset to the commit BEFORE (^) the first bad one
    git reset --soft 274c94^
    

    此时,我们刚刚撤消的三个“错误”提交中描述的修改位于您的工作树中,等待添加和提交

    # then switch branch to whichever branch suits your needs
    git checkout myBranch
    
    # Add and commit the way you usually do, for example :
    git add .
    git commit -m "Awesome message"