在特定提交之前删除提交

Eri*_*ner 21 git version-control

有没有办法在指定的提交之前删除所有提交并使用该提交作为初始提交?

Wal*_*ndt 25

假设新的最旧提交的哈希是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!
Run Code Online (Sandbox Code Playgroud)

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

编辑:测试和修复; 稍晚,精炼了一下

  • 请注意,"Killed history"成为初始提交的新提交消息.如果您想要其他内容,例如原始消息,只需将其传输到那里即可. (2认同)