You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在不丢失提交历史的情况下从Git仓库移除大文件

Fixing GitHub Push Rejection from Large MP3 (Preserving Commit History)

Got it, let's fix this properly. The issue here is that even though you deleted the file locally, it's still present in your Git commit history—so Git is still trying to push that historical version when you try to upload. A simple git reset won't cut it because that only affects the staging area, not past commits.

Here are the solutions based on where the large file was added:

Case 1: The large MP3 was added in your most recent commit

If this file was part of your last commit, you can amend that commit to remove the file without losing other changes:

  • First, confirm the file is already deleted locally (you've done this step). Run this to remove it from Git's tracking entirely:
    git rm --cached "client/build/audio/Celtic Music - Ancient Forest _ 3 hours of celtic fantasy music (192 kbps) (TubeMp3Convert.com).mp3"
    
    (The quotes are important here because the filename has spaces and special characters.)
  • Then update your last commit to exclude this file:
    git commit --amend --no-edit
    
    The --no-edit keeps your original commit message.
  • Now push your updated commit to GitHub:
    git push origin <your-branch-name>
    

Case 2: The large MP3 exists in older commits (before your last one)

If the file was added in an earlier commit, you'll need to rewrite your Git history to remove it entirely. We'll use git filter-repo (the official Git-recommended tool for this, available in Git 2.22+):

  1. First, back up your local repository (copy the entire folder to another location) to avoid data loss if something goes wrong.
  2. Run this command to remove the file from all past commits:
    git filter-repo --path "client/build/audio/Celtic Music - Ancient Forest _ 3 hours of celtic fantasy music (192 kbps) (TubeMp3Convert.com).mp3" --invert-paths
    
  3. Since you've rewritten history, you'll need to force-push to GitHub:
    git push origin <your-branch-name> --force
    
    ⚠️ Note: If other people are collaborating on this repository, tell them to re-clone the repo or rebase their work on the updated branch—force-pushing rewrites the remote history, so their local copies will be out of sync.

Prevent This From Happening Again

Add a .gitignore file to your repository (if you don't have one) and add this line to exclude audio files in that directory:

client/build/audio/*.mp3

This will stop Git from tracking any MP3 files in that folder in the future.

内容的提问来源于stack exchange,提问作者David Scherer-ODell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:46:03