如何借助BFG清理Unity项目GitHub仓库的冗余文件提交历史?
Hey there! Let's walk through how to clean up those pesky auto-generated Unity files from your repo's commit history, and confirm whether BFG Repo Cleaner is the right tool for the job.
Is BFG Repo Cleaner the Optimal Tool?
Absolutely. Compared to git filter-branch (which is slow and error-prone for large repos), BFG is built specifically for bulk Git history cleanup—removing unwanted files, trimming large assets, etc. It’s way faster, safer, and perfect for your use case where you need to eliminate multiple file types scattered across different directories.
Step-by-Step Cleanup (Leveraging Your Clean Baseline)
Since you already have a clean version of your repo with only the files you want, we can use that to guide the cleanup while preserving meaningful commit history. Here's how:
1. Prepare a Repository Mirror
First, clone a mirror of your repo (BFG works best with mirrors to avoid messing up your working copy):
git clone --mirror https://github.com/your-username/your-unity-repo.git
Pro tip: Make a full backup of this mirror folder before proceeding—better safe than sorry if something goes wrong.
2. Define Unwanted Files
Create a .gitignore file in the same directory as your mirror, listing all the file types and folders you want to remove. For Unity, this might look like:
# Unity auto-generated cruft *.meta Library/ Temp/ obj/ Build/ # Add any other unwanted patterns here
3. Run BFG to Scrub History
Download the BFG jar file, then run this command to remove all files matching your .gitignore patterns from every commit:
java -jar bfg.jar --delete-files-from-gitignore your-unity-repo.git
If you prefer targeting specific files directly without a .gitignore, use:
java -jar bfg.jar --delete-files "*.meta" --delete-folders Library,Temp your-unity-repo.git
This will scan every commit in your history and strip out all the unwanted files you specified.
4. Finalize Cleanup and Push Changes
Navigate into the mirror repo and run Git's cleanup commands to purge the now-unused history data:
cd your-unity-repo.git git reflog expire --expire=now --all git gc --prune=now --aggressive
Finally, push the cleaned history back to the remote repo:
git push
Critical Collaboration Notes
- History has been rewritten: Any collaborators on this repo will need to discard their local copies and clone the cleaned repo fresh, or run:
Warning: This will erase uncommitted local changes, so make sure everyone backs up their work first.git fetch origin git reset --hard origin/main # Replace main with your default branch name - Tags: If your repo has tags, BFG preserves them by default. To update tags to point to the cleaned commits, add the
--tagsflag to your BFG command.
Alternative: Starting Fresh with Your Baseline
If you don’t care about preserving old commit history (beyond your clean baseline), you could take a simpler route:
- Create a new empty repo on GitHub
- Push your clean baseline code to this new repo
- Update all collaborators to use the new repo instead
But this discards all past change tracking, so only do this if you don’t need to reference old commits (minus the unwanted files). For most cases, using BFG to keep meaningful history while removing cruft is the better choice.
内容的提问来源于stack exchange,提问作者PhoenixAki

