简体   繁体   中英

Git remove specific files from all previous commits

I started a small project (10mb) and as it grew and produced output images it grew exponentially (to 12 gb). I realize I was uploading my images which are not part of the code. I removed them, added a rule not to upload images and kept commiting. Problem is now the code is still a few mb but the whole repository weighs 2 gb. I would like to revert or remove all the changes in the history where I uploaded images I mean every time I sent

new file: image.png 

to the server it uploads the image, now I want to remove any trace of the images.

I am working in a brach on my own.

Can some one tell me how to do this?

It can be done with git rebase -i . it will open a dialog where you can alter every commit and remove the image from this commit

BUT

The side effect of rebase is that SHA-1 will be changes and if someone else already pulled out the branch it will become unusable for him, he will have to delete the branch and check it out again. so think twice before you do it.

How to find in which commit the file was modified?

git log --follow <file_name>  

How to remove files form a lot of commits?

Read this post . It has a detailed explanation + sample code on how to do it.

Best practice

Store big files in submodules so they will not be part of your base code

You can use git filter-branch . Github has a pretty good help page explaining the process of removing a file from the repository.

https://help.github.com/articles/remove-sensitive-data/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM