How to clear GIT history with BFG CleanerAdnan SabanovicBlockedUnblockFollowFollowingMar 30Clear your GIT historyThis article will have two suggestions.
How to clear your GIT history, which is something you would do if you made a lot of mistakes by committing large files to your GIT repo which now started to grow exponentially2.
A concrete example and my opinion on how to prevent your GIT from growing too large when using Laravel/VueJS in your mid/large projects.
So let’s start with #2 and then we will get to the actual GIT history clean up because I think this will be a great intro to that topic.
There are a lot of questions in the community whether you should commit your public resources (js and css files) or they should be built on the actual server.
The community is really split on this decision, which I understand completely, since everyone has different needs and not every project is the same size, purpose nor does it have the same history.
However if you have this hypothetical situation where you are developing a mid/large size project with Laravel, and then you start transitioning into having more and more browser action.
What does this mean?Well, imagine that your project starts as a prototype, then it transitions into a demo project and then you want to go live but you need it to be much more shiny so you start using VueJS with AJAX.
After that you start building a VueJS project on top of Laravel and you find yourself developing all these components and adding more and more third-party libraries, until your app.
js file really starts to grow.
(I won’t go into a lot of details on how project cycles go and the raw reality of what it means to build a project where you need to ramp up on features pretty quickly)At this point it is time to refactor, restructure your js files, separate what get’s to be built every time and what not, so you can split into multiple js files and load it in proper places where needed, instead of dumping everything in one app.
However before you do that, you will spend some time where with every new commit your webpack will build new app.
js and app.
css resource to the point where your commits are 5, 6, 7 MB heavy, each.
Over time this creates a lot of junk in your commit history and as it goes, GIT will keep all these files in its objects (packs) even if you remove it.
That’s the whole point of GIT so once something is removed, you can roll back your changes and you get that exact file.
Things don’t just appear like that.
GIT isn’t really magical.
Sorry if I disappointed you.
Anyways, this is a good opportunity to run a cleanup of your GIT history removing all the files from your history.
BFG Repo cleanerSo there is a script that can help you automate this really.
io/bfg-repo-cleaner/If you read its instructions, you will see that you need to have at least Java 7 installed on your computer in order for this to work.
I am using Mac btw, so once I install that I am ready to run the cleaner.
In the documentation, it says that you should clone your repo with the–mirror tag which isn’t really necessary in case you already have the project and have all the main branches.
Now imagine your repo path is this:/Users/adnan/my-git-repoYou need to download the bfg file from the above website and place the jar file inside the adnanfolder, right outside of my-git-repo.
Then what I usually do is that I rename the bfg file to bfg.
jar since it will have the version in the name once you download.
Then what you want to do is to runjava -jar bfg.
jar –strip-blobs-bigger-than 2M my-git-repoWhat this will do is to find all the files that are larger than 2MB and remove them.
With each removal, it will rewrite your commit hash.
You will get that mapping between the old and the new commits in case you need it.
The important thing to know is that your last commit is protected so it guarantees that all the files that are there will survive.
Once this is complete you need to physically remove those files (up until this point you only removed it from GIT).
You will do that if you rungit reflog expire –expire=now –all && git gc –prune=now –aggressiveAfter this, you need to rungit push origin <your-branch> –forceNow if you are on a hosted repo like Bitbucket, when you go to check the repo size, you will notice that it didn’t actually shrink.
What more, it grew even more.
Don’t panic !.
It basically added the new repo on top of the old one.
You need to run your garbage collector on the remote repo as well so it does the same thing as you did locally.
Again, if you are on a hosted repo like Bitbucket, you can ask the support to run it for you or you can do this hack where you remove the last git commitgit reset –hard HEAD~1and then git push –forceThat will force the remote git to run the garbage collector (git gc)That’s all folks.
Hope this helps someone.
If anyone needs to pull this new repo, then they should force the pull and do the git reset –hard origin <your-branch>.
Happy coding!Follow me on TwitterAdd me on LinkedIn.. More details