代码之家  ›  专栏  ›  技术社区  ›  Era

在特定提交之前删除提交

  •  19
  • Era  · 技术社区  · 16 年前

    有没有一种方法可以在指定的提交之前删除所有提交,并将该提交用作初始提交?

    2 回复  |  直到 9 年前
        1
  •  21
  •   Walter Mundt    16 年前

    假设新的最旧提交哈希是x,我们可以临时使用“oldroot”和“newroot”:

    git checkout -b oldroot X
    TREE=`git write-tree`
    COMMIT=`echo "Killed history" | git commit-tree "$TREE"`
    git checkout -b newroot "$COMMIT"
    git rebase --onto newroot oldroot master
    # repeat for other branches than master that should use the new initial commit
    git checkout master
    git branch -D oldroot
    git branch -D newroot
    git gc # WARNING: if everything's done right, this will actually delete your history from the repo!
    

    这将创建一个“newroot”提交,内容与“oldroot”提交相同,但没有任何父级。然后,它将所有其他分支重新定位到新的根上,这应该在所有分支的历史中。

    编辑:测试并修复;稍晚一点,改进一点

        2
  •  0
  •   Shubham Aggarwal    9 年前

    解决方案越简单, 考虑到原来你的分支已经承诺 主要的 你做了一个承诺 第一 ,现在你也做了一个承诺 第二 在顶部 第一 . 所以你有这样的东西:

    main->first->second
    

    现在你想要 第二 在顶部 主要的 而不是在 第一 . 类似:

    main->second->first or main->second
    

    你可以简单地做,

    git rebase -i main
    

    这将为您提供一个交互式shell,您可以在其中重新排列提交顺序或删除所选的任何提交。