我一直在尝试使用 git subtree 并遇到以下情况。
我使用 git subtree 将一个外部项目添加到我的 repo 中,我故意保留了上游项目的所有历史记录,因为我希望能够引用该项目的历史记录,并在以后为上游项目做出贡献。
事实证明,上游项目的另一个贡献者不小心将一个大文件推送到主分支。为了解决这个问题,上游项目重写了历史并强制推送到 master 上。在创建我的“monorepo”时,我包含了这个提交,我也想删除它。
如何更新我的存储库以反射(reflect)子树的新历史记录?
我的第一次尝试是使用 filter-branch 来完全删除子树和所有历史记录。
git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch upstream-project-dir' --prune-empty HEAD
删除旧版本的子树后,我可以使用新的上游主节点重新添加子树。但是,这不起作用,因为出于某种原因,提交历史记录仍然显示在 git log 输出中。
更新
我已经编写了创建最小可重现示例的步骤。
git init test-monorepo
cd ./test-monorepo
echo hello world > README
git add README
git commit -m 'initial commit'
git remote add thirdparty git@github.com:teivah/algodeck.git
git fetch thirdparty
git subtree add --prefix algodeck thirdparty master
echo dont panic >> algodeck/README.md
git commit -a -m 'test commit'
git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch algodeck' --prune-empty HEAD
git log
最佳答案
git fetch upstream
# using one or more of the following commands :
git rebase --interactive
git filter-branch
...
通过这两个步骤,您的存储库中的任何提交将不再引用大文件。
它还会在某个时间点从您的硬盘驱动器中删除,当 git 运行其垃圾收集器并且已达到悬空 blob 的过期延迟时。
如果您迫切需要尽快从硬盘中删除这个大文件:
手动运行
git gc --prune=now
关于git - 在上游项目强制推送到 master 后,如何修复 git 子树?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58750777/