背景
要在 Git 历史中删除大文件,但保留当前工作目录中的文件,可以使用以下步骤:
-
安装 BFG Repo-Cleaner:这个工具专门用于清理 Git 仓库中的大文件。
下载并安装 BFG Repo-Cleaner:
-
备份你的仓库:操作之前务必备份你的仓库以防意外。
git clone --mirror https://github.com/your/repo.git cd repo.git -
使用 BFG 清理大文件:
例如,要删除超过 100MB 的文件,可以运行:
bfg --strip-blobs-bigger-than 100MBFG 会将所有大于 100MB 的文件标记为删除。
-
清理 Git 仓库:
使用 BFG 之后,运行以下命令以彻底删除那些大文件:
git reflog expire --expire=now --all && git gc --prune=now --aggressive -
强制推送到远程仓库:
注意,强制推送会覆盖远程仓库的历史,因此请确保所有协作者都已同意此操作。
git push --force
完整示例
以下是详细的步骤示例:
# Step 1: 克隆仓库
git clone --mirror https://github.com/your/repo.git
cd repo.git
# Step 2: 使用 BFG 清理大文件
bfg --strip-blobs-bigger-than 100M
# Step 3: 清理 Git 仓库
git reflog expire --expire=now --all && git gc --prune=now --aggressive
# Step 4: 强制推送到远程仓库
git push --force这样你就成功地从 Git 历史记录中删除了大文件,同时保留了当前工作目录中的文件。