I was working on a repository on my GitHub account and this is a problem I stumbled upon.
- Node.js project with a folder with a few npm packages installed
- The packages were in
node_modules
folder - Added that folder to git repository and pushed the code to github (wasn't thinking about the npm part at that time)
- Realized that you don't really need that folder to be a part of the code
- Deleted that folder, pushed it
At that instance, the size of the total git repo was around 6MB where the actual code (all except that folder) was only around 300 KB.
Now what I am looking for in the end is a way to get rid of details of that package folder from git's history so if someone clones it, they don't have to download 6mb worth of history where the only actual files they will be getting as of the last commit would be 300KB.
I looked up possible solutions for this and tried these 2 methods
- Remove file from git repository (history)
- http://help.github.com/remove-sensitive-data/
- https://gist.github.com/1588371
The Gist seemed like it worked where after running the script, it showed that it got rid of that folder and after that it showed that 50 different commits were modified. But it didn't let me push that code. When I tried to push it, it said Branch up to date
but showed 50 commits were modified upon a git status
. The other 2 methods didn't help either.
Now even though it showed that it got rid of that folder's history, when I checked the size of that repo on my localhost, it was still around 6MB. (I also deleted the refs/original
folder but didn't see the change in the size of the repo).
What I am looking to clarify is, if there's a way to get rid of not only the commit history (which is the only thing I think happened) but also those files git is keeping assuming one wants to rollback.
Lets say a solution is presented for this and is applied on my localhost but cant be reproduced to that GitHub repo, is it possible to clone that repo, rollback to the first commit perform the trick and push it (or does that mean that git will still have a history of all those commits? - aka. 6MB).
My end goal here is to basically find the best way to get rid of the folder contents from git so that a user doesn't have to download 6MB worth of stuff and still possibly have the other commits that never touched the modules folder (that's pretty much all of them) in git's history.
How can I do this?
I removed the bin and obj folders from old C# projects using git on windows. Be careful with
It destroys the integrity of the git installation by deleting the usr/bin folder in the git install folder.
I find that the
--tree-filter
option used in other answers can be very slow, especially on larger repositories with lots of commits.Here is the method I use to completely remove a directory from the git history using the
--index-filter
option, which runs much quicker:You can check the size of the repository before and after the
gc
with:The best and most accurate method I found was to download the bfg.jar file: https://rtyley.github.io/bfg-repo-cleaner/
Then run the commands:
If you want to delete files then use the delete-files option instead:
In addition to the popular answer above I would like to add a few notes for Windows-systems. The command
works perfectly without any modification! Therefore, you must not use
Remove-Item
,del
or anything else instead ofrm -rf
.If you need to specify a path to a file or directory use slashes like
./path/to/node_modules
Complete copy&paste recipe, just adding the commands in the comments (for the copy-paste solution), after testing them:
After this, you can remove the line "node_modules/" from .gitignore
If you are here to copy-paste code:
This is an example which removes
node_modules
from history