如何删除旧的git历史记录? [英] How to delete the old git history?
问题描述
我有很多(2000多个)提交的git存储库,例如:
I have git repository with many, many (2000+) commits, for example:
l-- m -- n
/
a -- b -- c -- d -- e -- f -- g -- h -- i -- j -- k
\
x -- y -- z
并且我想截断旧的日志历史记录-从日志历史记录中删除所有提交(例如,从提交"f"开始,但作为存储库的开头).
and I want to truncate old log history - delete all commits from log history starting from (for example) commit "f" but as the beginning of repository.
该怎么做?
推荐答案
为了不丢失一些历史记录;最好先复制您的存储库副本:).我们开始:(<f>
是您想要成为新的根提交的提交f的阴影)
In order not to lose some history; better first take a copy of your repository :). Here we go: (<f>
is the sha of the commit f that you want to be the new root commit)
git checkout --orphan temp <f> # checkout to the status of the git repo at commit f; creating a branch named "temp"
git commit -m "new root commit" # create a new commit that is to be the new root commit
git rebase --onto temp <f> master # now rebase the part of history from <f> to master onthe temp branch
git branch -D temp # we don't need the temp branch anymore
如果您有一个要在其中具有相同的截断历史记录的遥控器,请执行以下操作:您可以使用git push -f
. 警告:这是一个危险的命令;不要轻易使用它!如果您要确保代码的最新版本仍然相同,请执行以下操作:您可以运行git diff origin/master
.那应该不显示任何更改(因为仅更改了历史记录;不更改了文件的内容).
If you have a remote where you want to have the same truncated history; you can use git push -f
. Warning this is a dangerous command; don't use this lightly! If you want to be sure that your last version of the code is still the same; you can run git diff origin/master
. That should show no changes (since only the history changed; not the content of your files).
git push -f
以下2条命令是可选的-它们可以使您的git repo保持良好状态.
The following 2 commands are optional - they keep your git repo in good shape.
git prune --progress # delete all the objects w/o references
git gc --aggressive # aggressively collect garbage; may take a lot of time on large repos
这篇关于如何删除旧的git历史记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!