2

so I work on a branch of a master git repository, which is now 4 GB large, because I once pushed raw data by mistake. So what I did is to use git reset--hard@(10) to go back 10 commits and then used git push -f. Now my commit history is clean, but the repo is still 4gb large.

How can I delete the last 10 commits. To be honest I wouldn't even mind setting back my branch completely to the same state as the master is.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Abu Bati5a
  • 109
  • 1
  • 9
  • 2
    That will be dealt with eventually [by the garbage collector](https://stackoverflow.com/questions/3162786/how-can-i-trigger-garbage-collection-on-a-git-remote-repository). Unless you definitely need to reclaim that storage space **right now** you probably don't need to do anything. – Joachim Sauer Dec 19 '19 at 11:23
  • @JoachimSauer Thanks for your answer. Actually I would really like to get the storage right now, as I am already dealing with this issue since 2 days. I tried to run ``git gc``, still nothing – Abu Bati5a Dec 19 '19 at 12:06
  • Basically `git gc` should work, but if you already pushed to a remote repository, you need to execute `git gc` on **that** repository (as it's not an operation that changes anything about what you push). – Joachim Sauer Dec 19 '19 at 12:21
  • https://stackoverflow.com/search?q=%5Bgit%5D+reduce+repository+size – phd Dec 19 '19 at 13:42
  • Hi so basically my local copy of the branch is shrinked to 170mb, but on git it says the project is still 4GB large. However, when I download it, it is only 170mb as well.... any suggestions ? – Abu Bati5a Dec 19 '19 at 15:33
  • @AbuBati5a: "on git"? What do you mean? GitHub? GitLab? – Joachim Sauer Dec 20 '19 at 09:56
  • GitHub runs gc on their servers periodically. Just sit back and relax. On GitLab, do Settings > General > Advanced > Housekeeping; see https://docs.gitlab.com/ee/administration/housekeeping.html – cfstras Dec 20 '19 at 16:43

2 Answers2

5

Git has a reflog which keeps references to the commits and HEADs you have been on for some time. These entries still hold links to your commits, and so git gc won't delete them (yet).

You can force that to happen by running the following:

git reflog expire --expire=now --all
git gc --prune=now
cfstras
  • 1,613
  • 15
  • 21
  • Hi so basically my local copy of the branch is shrinked to 170mb, but on git it says the project is still 4GB large. However, when I download it, it is only 170mb as well.... any suggestions ? – Abu Bati5a Dec 19 '19 at 15:33
1

I had the same problem couple of months ago. Then i found an awesome tool to clean the git repo. Its really working well.

https://rtyley.github.io/bfg-repo-cleaner/

Ahamed Safnaj
  • 611
  • 1
  • 6
  • 18