6

There is such question albeit old one, but it didn't seem to help. I have repo. In the repo I have a gif file which is roughly 6MB. It happenned so that I pushed different versions of this GIF and apparently all of them are stored in .git folder, which made the size of git folder around 40MB.

From the project folder I tried running as suggested in linked question:

   git repack -a -d --depth=250 --window=250

But it didn't affect the size of .git folder (do I have to push to see the size reduced?). Is there something I can do to reduce .git folder size?

Also trying git gc didn't seem to reduce .git folder size.

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
  • Related: https://git-scm.com/book/en/v2/Git-Internals-Maintenance-and-Data-Recovery – k0pernikus Dec 03 '18 at 16:03
  • @k0pernikus does it contain an answer? I will go through that .... – Giorgi Moniava Dec 03 '18 at 16:08
  • GIF image files are already compressed, and Git's internal compression tricks that normally keep a repository from growing rapidly with each new version of a file simply don't work on already-compressed files. So if the image is 6 MB and you've put in 5 variants of it, you should *expect* this to use about 6*5 = 30 MB right there. – torek Dec 03 '18 at 16:13
  • The part starting with: `Removing Objects` should get your started in the article https://git-scm.com/book/en/v2/Git-Internals-Maintenance-and-Data-Recovery That beig said, I consider 40MB to be tiny and you have to be aware since git stores the entire history, you will rewrite your repository's history in the process. – k0pernikus Dec 03 '18 at 16:15
  • 1
    Different use-case, same solution: https://stackoverflow.com/q/872565/457268 – k0pernikus Dec 03 '18 at 16:17
  • Somebody also created a script for that: [git forget blob](https://ownyourbits.com/2017/01/18/completely-remove-a-file-from-a-git-repository-with-git-forget-blob/) – k0pernikus Dec 03 '18 at 16:19
  • @k0pernikus I will look into your links just for the record I don't want to remove the current GIF file, just the old versions of it. – Giorgi Moniava Dec 03 '18 at 16:37
  • `git rm --cached ` and `git add ` – Peter Dec 03 '18 at 16:43
  • @Peter you sure that would work? Seems to simplistic compared to all the answers I got above. – Giorgi Moniava Dec 03 '18 at 16:46
  • @giorgim it is more like an idea, it removes the file from git and add it back. I am not 100% sure about the .git folder. – Peter Dec 03 '18 at 16:49
  • related: https://confluence.atlassian.com/bitbucket/reduce-repository-size-321848262.html `Delete files by name` part – Peter Dec 03 '18 at 16:49
  • @k0pernikus will there be an easier solution if I don't care about history? e.g. maybe remove .git folder altogether? – Giorgi Moniava Dec 03 '18 at 16:58
  • @giorgim The `.git` folder *is* your local git-repository. You should not delete that. – k0pernikus Dec 03 '18 at 17:03

1 Answers1

11

A hacky solution:

git push # ensure that you push all your last commits from all branches, and
         # take care about your stashes as well because we are going to delete
         # everything.
cd ..
rm -rf online-shop
git clone --depth 1 git@github.com:giorgi-m/online-shop.git

This last line will clone the repository with only a one commit history.

Hence your .git folder will be much lighter. However, you will not have the whole history on your computer and this may not be what you are looking for.

For other users that would like to clone your application, you can tell them in the README file that they can fasten download by using the next command:

git clone --depth 1 git@github.com:giorgi-m/online-shop.git

Another solution, which is rewriting history, would be to remove all your remote history. You can see more about it in this answer:

Deleting the .git folder may cause problems in your git repository. If you want to delete all your commit history but keep the code in its current state, it is very safe to do it as in the following:

Checkout

git checkout --orphan latest_branch

Add all the files

git add -A

Commit the changes

git commit -am "commit message"

Delete the branch

git branch -D master

Rename the current branch to master

git branch -m master

Finally, force update your repository

git push -f origin master

PS: this will not keep your old commit history around

Ulysse BN
  • 10,116
  • 7
  • 54
  • 82
  • history isn't important. After the clone, should I call push again, so that changes are reflected online? – Giorgi Moniava Dec 04 '18 at 11:38
  • Also, does it suffer from this problem: "Keep in mind that once you've pushed this code to a remote repository like GitHub and others have cloned that remote repository, you're now in a situation where you're rewriting history. When others try pull down your latest changes after this, they'll get a message indicating that the the changes can't be applied because it's not a fast-forward.". From here: https://stackoverflow.com/questions/872565/remove-sensitive-files-and-their-commits-from-git-history – Giorgi Moniava Dec 04 '18 at 11:41
  • I'm not sure I get this question well. My answer is no, since you already pushed everything **before** deleting. (that is **really important to do**, since you `rm -rf`). – Ulysse BN Dec 04 '18 at 11:41
  • It doesn't: you're not rewritting history, you're just getting a compacted history locally. The remote doesn't change. A quick hint about it: if you don't see `--force` or `--force-with-lease` in a `git push` commit, then it cannot rewrite history. – Ulysse BN Dec 04 '18 at 11:43
  • And if you want to delete your history on github as well, you could follow this answer: https://stackoverflow.com/a/26000395/6320039. However, you will **definitely loose your whole history** – Ulysse BN Dec 04 '18 at 11:47
  • Yes but my goal was to reduce the size of .git folder as it is online because it takes long for users to download. – Giorgi Moniava Dec 04 '18 at 11:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184688/discussion-between-ulysse-bn-and-giorgim). – Ulysse BN Dec 04 '18 at 11:48
  • @gmoniava there is yet another solution, you could use [git lfs](https://git-lfs.github.com/) for your gif image. It helps storing binary data more efficiently, keeping only a hash in the git repo. If this gets 5 or more upvotes I'll consider adding details to this idea :) – Ulysse BN Apr 07 '21 at 06:50