gpt4 book ai didi

git - 删除本地存储库中的历史记录,而不是使用 --depth 1 再次克隆它

转载 作者:太空狗 更新时间:2023-10-29 13:15:35 25 4
gpt4 key购买 nike

基本上是标题。

我只是将一些东西与我的 .gitignore 混合在一起,并将我的 .git 目录膨胀到 > 100mb(这是一个总共有大约 100 次提交的 repo..)。

所以我是个懒惰的人,想知道是否可以删除我的历史记录。我不想 rebase 某些东西,我只想缩小我的 .git 目录大小。

当没有办法做到这一点时,我将 git clone --depth 1 我的 repo 来获得相同的效果。

最佳答案

这是两个相互冲突的目标:

  • 删除历史意味着一个全新的 repo(rm -Rf .gitgit init .):这意味着你 使用 git push --force 强制推送你的新本地仓库,同时破坏远程仓库上的历史记录,

  • 保持远程仓库完整意味着在本地使用 git clone --depth 1,即 safe to do since Git 2.0, as I have documented here (即,您可以从本地浅层 repo 推回到远程 repo )

第二种方法似乎最安全:

  • 保存远程仓库
  • 在本地优化磁盘空间

注意:对于具有完整历史记录的本地仓库,git gcgit repack仅靠这些还不足以真正缩小 .git 文件夹的大小。
参见“git gc --aggressive vs git repack”。

git gc
git repack -Ad # kills in-pack garbage
git prune # kills loose garbage

(这对新克隆的 repo 没有多大作用,它只对你已经工作了一段时间的本地 repo 有效)另外,如果你在过去做过类似 git filter-branch 的操作,你的 .git 文件夹仍然会保留一个像 .git/refs/original/ 这样的文件夹,如果你想减小 .git 的大小,你需要删除它/


AFAIU the original question and how I want to do it is precisely without removing .git folder, just removing local copy of git log data to make its' state identical to the state after git clone --depth 1

参见“Converting git repository to shallow?

你可以试试:

cd /my/repo
git show-ref -s HEAD > .git/shallow
git reflog expire --expire=0
git prune
git prune-packed

关于git - 删除本地存储库中的历史记录,而不是使用 --depth 1 再次克隆它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33906288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com