2010-06-17 54 views
18

有没有办法在指定提交之前移除所有提交并将该提交用作初始提交?在特定提交之前移除提交

+0

我知道我已经看过一个教程在某处做类似的事情......它展示了如何让历史回购结束于提交X,然后将提交X提交到HEAD并从它们开始新的回购,在X上注释关于如何访问历史回购。 我现在找不到它,但它是可能的。 – Daenyth 2010-06-17 15:41:34

回答

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

请注意,“Killed History”成为初始提交的新提交消息。如果你想要别的东西,比如原始信息,就把它放在那里。 – 2010-06-17 16:26:34

0

更容易的解决办法是, 考虑最初你的分支有提交主要和你没有提交第一,现在还没有一个上第一顶提交第二。所以,你有这样的:

main->first->second 

现在你想对的主要顶部第二上,而不是第一顶部。喜欢的东西:

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

你可以简单地做,

git rebase -i main 

这会给你一个交互式shell,你可以重新提交的顺序或删除任何提交您所选择的。