Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove old versions of media files from a git repository

Tags:

I have a Git repository with several huge media files (images and audio files). Several versions of these media files have been successively commited to the repo. The files are successively refined versions of the same assets, and they have the same name.

I want to keep only the latest version in the Git repository, because it is becoming too big.
What is the simplest way to do this?
How can I propagate these changes correctly to the upstream repository?

like image 898
Ricardo Sanchez-Saez Avatar asked Jun 15 '11 13:06

Ricardo Sanchez-Saez


People also ask

How do I remove old versions from git?

As far as I know, this can't be done, because in git, every commit depends on the contents of the entire history up to that point. So the only way to get rid of the old, big files would be to "replay" the entire commit history (preferrably with the same commit timestamps and authors), omitting the big files.

How do I remove all files from a git repository?

Just run the rm command with the -f and -r switch to recursively remove the . git folder and all of the files and folders it contains. This Git repo remove command also allows you to delete the Git repo while allowing all of the other files and folder to remain untouched.


1 Answers

Old thread but in case someone else stumbles along here…

GitHub & Bitbucket both recommend using BFG Repo-Cleaner.

See:
GitHub: Remove Sensitive Data
Bitbucket: Reduce Repository Size & Bitbucket: Maintaining a Git Repository

Example to remove files over 1 Megabyte, as well as jpgs, pngs and mp3s that are not in HEAD:

# First get the latest bfg.jar, then: $ git clone --mirror git://example.com/some-big-repo.git $ java -jar bfg.jar --strip-blobs-bigger-than 1M --delete-files '*.{jpg,png,mp3}' some-big-repo.git $ cd some-big-repo.git $ git reflog expire --expire=now --all && git gc --prune=now --aggressive $ git push 

Note: now you've pushed the updated revs, the remote repository should also run it's git gc …else you won't see the size reduction. (see e.g. https://stackoverflow.com/a/28782154/3419541)

Finally, re-clone the repository to be sure that you don't accidentally re-commit the old media file blobs.

like image 147
lac.alan Avatar answered Oct 21 '22 13:10

lac.alan