I have a git project but the problem is that the directory ".git" is too big. Indeed, my whole project is 3MB but the ".git" is 36MB. Why is it so big? How can I clean it?
-
Could be that there used to be large files in the repository, but it was deleted in one of the commits. Check out [BFG Repo cleaner](https://rtyley.github.io/bfg-repo-cleaner/). – D Malan Apr 02 '20 at 20:10
-
2This might help: [How to remove/delete a large file from commit history in Git repository?](https://stackoverflow.com/q/2100907/4518341) – wjandrea Apr 02 '20 at 20:10
1 Answers
Remember that every time you do a commit, every changed file in the commit takes up space in the repository. Even if the change itself is just one letter, the whole file is there, at least twice (before the change and after the change). The entire history, the state of every file in every commit, even if that file doesn't visibly "exist" any more at the end of the branch where you are working, is in the repo!
Let's say you have a 1MB jpg image and you change it 36 times and commit each time. Bingo, that's (at least) 36MB in the repo. And if you now delete that jpg image and commit, the repo won't get any smaller because of that.
I use jpg as my example on purpose; usually it is binaries that take up a lot of space. Git can save some space with regard to text files, but it can't do that with binaries.
Having said that, however, I should also say: 36MB is not big. It's pretty big, but it's not atypical of how big a repository can be for a large project with a long history.

- 515,959
- 87
- 875
- 1,141
-
4Also worth mentioning: Git will generate, then leave behind, some "trash" or "garbage" objects. Periodically, Git runs a *garbage collector*, `git gc`, to automatically clean up. Its choice of cleanup interval might not always match your personal preferences (e.g., if you think 36 MB is big :-) ). You can run it manually—just run `git gc`—to get it to do a cleanup right now. However, running it more often than necessary is somewhat counterproductive: it's more efficient when it takes on more stuff at once. – torek Apr 02 '20 at 21:47