Tutorial: Safely Reducing the Size of a Git Repository

Before starting any of these procedures, make sure to backup your repository.

Tutorial: Safely Reducing the Size of a Git Repository

Prerequisites:
  • A local Git repository.
  • Backup the repository before making changes.
  • Optional: Install tools like BFG Repo-Cleaner.
Step 1: Basic Repository Cleanup

1.1. Run Git Garbage Collection

Start by running the Git built-in garbage collection command, which can help clean up unnecessary files and optimize the repository.

bash 复制代码
cd /path/to/your/repo
git gc --aggressive --prune=now
  • --aggressive: More thorough cleanup.
  • --prune=now: Removes objects that are no longer needed.

1.2. Clean Reflogs

Reflogs record when the tips of branches and other references were updated in the repo. They can consume space, especially in large projects.

bash 复制代码
git reflog expire --expire=now --all
Step 2: Identify and Remove Large Files

2.1. Find Large Files

Use a script to find large files in your repository's history.

bash 复制代码
git rev-list --objects --all |
git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' |
sort -n -k 3 |
tail -n 10

This command will list the top 10 largest objects in the repo.

2.2. Remove Large Files Using BFG

If you find large files that should not be in the repository, use BFG Repo-Cleaner, which is faster and simpler than git filter-branch.

First, download and run BFG:

bash 复制代码
java -jar bfg.jar --strip-blobs-bigger-than 100M /path/to/your/repo

2.3. Alternative: Use git filter-branch

If you prefer not to use BFG, you can manually remove large files with git filter-branch:

bash 复制代码
git filter-branch --force --index-filter \
  "git rm --cached --ignore-unmatch PATH_TO_LARGE_FILE" \
  --prune-empty --tag-name-filter cat -- --all

Replace PATH_TO_LARGE_FILE with the path to the file you wish to remove.

Step 3: Clone the Repository Afresh

After cleaning up the history, it might be beneficial to clone the repository afresh to start with a new, smaller .git directory.

bash 复制代码
cd ..
git clone --mirror /path/to/old/repo new-repo
cd new-repo
git reflog expire --expire=now --all
git gc --aggressive --prune=now
Step 4: Replace Old Repository

Once you are satisfied with the new repository's state, you can replace the old repository:

bash 复制代码
mv /path/to/old/repo /path/to/old/repo-old
mv new-repo /path/to/old/repo
Final Notes
  • After performing these actions, especially if you changed the history, you will need to force-push to any remotes and inform collaborators to re-clone the repository.
  • Always ensure you have backups and confirm that no critical data is lost during the cleanup.

This tutorial will guide you through reducing the size of your Git repository effectively and safely. Remember, these changes affect the repository's history, which can impact collaborative workflows.

相关推荐
西瓜本瓜@3 小时前
在Android中如何使用Protobuf上传协议
android·java·开发语言·git·学习·android-studio
4Forsee6 小时前
【Git】从本地存档到协作开发的Git简单使用
git
Э时间行者于我8 小时前
git同时删除多个分支
git
我的golang之路果然有问题15 小时前
给git配置SSH(github,gitee)
经验分享·笔记·git·学习·gitee·ssh·github
漫步企鹅16 小时前
[Git] Git Stash 命令详解
git·git push·git pull·git commit·git pull rebase
船长@Quant18 小时前
协作开发攻略:Git全面使用指南 — 第二部分 高级技巧与最佳实践
git·版本控制·源代码管理·协作开发
用户126538387051220 小时前
github 和 gitee 配置问题及相关问题解决
git·github
极小狐21 小时前
极狐GitLab Git LFS 速率限制如何设置?
运维·git·ssh·gitlab·github
极小狐21 小时前
如何解决极狐GitLab 合并冲突?
人工智能·git·机器学习·gitlab
一袋米扛几楼9821 小时前
【GIT】github中的仓库如何删除?
git·github