Skip to content

git 如何给 git 仓库瘦身减少 .git 目录的大小

我把一个仓库大小从 220MB 减少到 14MB。

一、需求背景

我把一个很大的文件夹public/download添加到了.git 跟踪,已经 git commit 提交了,.git 目录回变得很大 220MB

后来我意识到错误,我把public/download添加到 .gitignore 配置文件中,并且执行命令 git rm -r --cached public/download 移除了管理,但是发现.git 文件还是很大

二、期望.git目录变小

希望 public/download 目录彻底从 .git 目录中删除,使得 .git 目录变得更小

三、怎么样可以做到

你可以使用以下步骤彻底从 Git 历史中删除 public/download 目录及其文件,从而减小 .git 目录的大小:

四、使用 git filter-repo

git filter-repo 是一个 Git 官方推荐的工具,可以更高效地处理这种情况。首先需要在 github 下载这个工具:

进入这个 GitHub releases 页面: https://github.com/newren/git-filter-repo/releases/

或者直接点击下面链接下载:

https://github.com/newren/git-filter-repo/releases/download/v2.45.0/git-filter-repo-2.45.0.tar.xz

解压压缩包,并复制这两个文件 git_filter_repo.py git_filter_repo 到你的 Git 安装目录的\bin目录下

我的 Git 安装目录是这个 C:\Program Files\Git,所以复制到这个目录下C:\Program Files\Git\bin

alt text

alt text

五、彻底删除 public/download 目录

警告 警告 警告

!!! 执行下面命令之前已经做好备份 .git 仓库

!!! 执行下面命令之前已经做好备份 .git 仓库

!!! 执行下面命令之前已经做好备份 .git 仓库

接下来,运行以下命令从所有的 Git 提交历史中删除 public/download 目录:

下面命令,在的项目根目录下的 PowerShell 运行:

powershell
python "C:\Program Files\Git\bin\git_filter_repo.py" --path public/download --invert-paths --force

这个命令的意思是:从所有历史记录中移除 public/download 目录。

alt text

alt text

提示我的 python 版本如下

powershell
PS C:\Users\Administrator\Desktop\ffffee> python --version
Python 3.9.12
PS C:\Users\Administrator\Desktop\ffffee>

如果不加 --force 命令将会提示有如下提示:

powershell
PS C:\Users\Administrator\Desktop\ffffee> Invoke-Expression 'python "C:\Program Files\Git\bin\git_filter_repo.py" --path public/download --invert-paths'
Aborting: Refusing to destructively overwrite repo history since
this does not look like a fresh clone.
  (expected freshly packed repo)
Please operate on a fresh clone instead.  If you want to proceed
anyway, use --force.

命令执行成功情况

powershell
PS C:\Users\Administrator\Desktop\ffffee> python "C:\Program Files\Git\bin\git_filter_repo.py" --path public/download --invert-paths --force
Parsed 14 commits
New history written in 0.12 seconds; now repacking/cleaning...
Repacking your repo and cleaning out old unneeded objects
HEAD is now at b629a7f feat: 支持docker 一键构建部署
Enumerating objects: 143, done.
Counting objects: 100% (143/143), done.
Delta compression using up to 32 threads
Compressing objects: 100% (134/134), done.
Writing objects: 100% (143/143), done.
Total 143 (delta 43), reused 0 (delta 0), pack-reused 0
Completely finished after 0.82 seconds.

六、推送到远程仓库

⚠️ 注意:

此操作会更改 Git 历史,并且所有克隆了此仓库的用户都需要重新克隆或重置他们的本地仓库。

所以,在执行此操作之前,确保通知其他开发者。

删除大文件后,你需要将修改后的 Git 历史强制推送到远程仓库:

当你git push的时候发现,remote 配置已经没有,需要重新配置,请看以下日志信息:

powershell
PS C:\Users\Administrator\Desktop\ffffee> git push
fatal: No configured push destination.
Either specify the URL from the command-line or configure a remote repository using

    git remote add <name> <url>

and then push using the remote name

    git push <name>

PS C:\Users\Administrator\Desktop\ffffee> git remote -v
PS C:\Users\Administrator\Desktop\ffffee> git remote add origin git@gitee.com:fe521/ffffee.com.git
PS C:\Users\Administrator\Desktop\ffffee> git push
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin main

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.

PS C:\Users\Administrator\Desktop\ffffee>     git push --set-upstream origin main
Enumerating objects: 60, done.
Counting objects: 100% (60/60), done.
Delta compression using up to 32 threads
Compressing objects: 100% (46/46), done.
Writing objects: 100% (52/52), 11.31 MiB | 9.03 MiB/s, done.
Total 52 (delta 10), reused 46 (delta 6), pack-reused 0
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 8f400382
To gitee.com:fe521/ffffee.com.git
   6fb5d07..b629a7f  main -> main
branch 'main' set up to track 'origin/main'.
PS C:\Users\Administrator\Desktop\ffffee>

可以快速拷贝下面的命令:

powershell
git push
powershell
git push
powershell
git remote -v
powershell
git remote add origin git@gitee.com:fe521/ffffee.com.git
powershell
git push
powershell
git push --set-upstream origin main

七、清理本地缓存

为了确保本地仓库的 .git 目录大小减少,执行以下命令清理垃圾数据:

bash
git gc --aggressive --prune=now

这将强制 Git 进行垃圾回收,并删除已无效的对象。

powershell
PS C:\Users\Administrator\Desktop\ffffee> git gc --aggressive --prune=now
Enumerating objects: 143, done.
Counting objects: 100% (143/143), done.
Delta compression using up to 32 threads
Compressing objects: 100% (134/134), done.
Writing objects: 100% (143/143), done.
Total 143 (delta 45), reused 98 (delta 0), pack-reused 0
PS C:\Users\Administrator\Desktop\ffffee>