如何从 git 存储库中删除旧历史? [英] How do I remove the old history from a git repository?
问题描述
恐怕我找不到与此特定场景相似的内容.
I'm afraid I couldn't find anything quite like this particular scenario.
我有一个历史悠久的 git 存储库:500 多个分支,500 多个标签,可以追溯到 2007 年年中.它包含约 19,500 次提交.我们希望删除 2010 年 1 月 1 日之前的所有历史记录,以使其更小且更易于处理(我们将在存档存储库中保留一份完整的历史记录副本).
I have a git repository with a lot of history: 500+ branches, 500+ tags, going back to mid-2007. It contains ~19,500 commits. We'd like to remove all of the history before Jan 1, 2010, to make it smaller and easier to deal with (we would keep a complete copy of the history in an archive repository).
我知道我想要成为新存储库根的提交.但是,我无法找出正确的 git mojo 来截断 repo 以从该提交开始.我猜
I know the commit that I want to have become the root of the new repository. I can't, however, figure out the correct git mojo to truncate the repo to start with that commit. I'm guessing some variant of
git filter-branch
涉及移植物是必要的;可能还需要分别处理我们想要保留的 200 多个分支中的每一个,然后将 repo 重新组合在一起(我做知道该怎么做).
involving grafts would be necessary; it might also be necessary to treat each of the 200+ branches we want to keep separately and then patch the repo back together (something I do know how to do).
有没有人做过这样的事情?如果重要的话,我有 git 1.7.2.3.
Has anyone ever done something like this? I've got git 1.7.2.3 if that matters.
推荐答案
您可以创建一个 graft 新根提交的父提交给无父提交(或空提交,例如存储库的真实根提交).例如.echo "
You can create a graft of the parent of your new root commit to no parent (or to an empty commit, e.g. the real root commit of your repository). E.g. echo "<NEW-ROOT-SHA1>" > .git/info/grafts
创建移植后,立即生效;您应该能够查看 git log
并看到不需要的旧提交已经消失:
After creating the graft, it takes effect right away; you should be able to look at git log
and see that the unwanted old commits have gone away:
$ echo 4a46bc886318679d8b15e05aea40b83ff6c3bd47 > .git/info/grafts
$ git log --decorate | tail --lines=11
commit cb3da2d4d8c3378919844b29e815bfd5fdc0210c
Author: Your Name <your.email@example.com>
Date: Fri May 24 14:04:10 2013 +0200
Another message
commit 4a46bc886318679d8b15e05aea40b83ff6c3bd47 (grafted)
Author: Your Name <your.email@example.com>
Date: Thu May 23 22:27:48 2013 +0200
Some message
如果一切正常,您可以使用 git filter-branch -- --all
使其永久化.
If all looks as intended, you can utilize git filter-branch -- --all
to make it permanent.
注意:在执行filter-branch 步骤后,所有提交 ID 都将发生变化,因此使用旧存储库的任何人都不得与使用新存储库的任何人合并.
BEWARE: after doing the filter-branch step, all commit ids will have changed, so anybody using the old repo must never merge with anyone using the new repo.
这篇关于如何从 git 存储库中删除旧历史?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!