How can I convert an already cloned git repository to a shallow repository?
The git repository is downloaded through a script outside of my control so I cannot do a shallow clone.
The reason for doing this is to save disk space. (Yes, I'm really short on disk space so even though a shallow repository doesn't save much, it is needed.)
I already tried
git repack -a -d -f -depth=1
But that actually made the repository larger.
A shallow repository has an incomplete history some of whose commits have parents cauterized away (in other words, Git is told to pretend that these commits do not have the parents, even though they are recorded in the commit object).
Definition. Shallow commits do have parents, but not in the shallow repo, and therefore grafts are introduced pretending that these commits have no parents.
Git's solution to the problem is shallow clone where you can use clone depth to define how deep your clone should go. For example, if you use –depth 1, then during cloning, Git will only get the latest copy of the relevant files. It can save you a lot of space and time.
This worked for me:
git pull --depth 1 git gc --prune=all
This leaves the tags and reflog laying around, each of which reference additional commits that can use up space. Note that I would not erase the reflog unless severely needed: it contains local change history used for recovery from mistakes.
There are commands on how to erase the tags or even the reflog in the comments below, and a link to a similar question with a longer answer.
If you still have a lot of space used you may need to remove the tags, which you should try first before removing the reflog.
You can convert git repo to a shallow one in place along this lines:
git show-ref -s HEAD > .git/shallow git reflog expire --expire=0 git prune git prune-packed
Make sure to make backup since this is destructive operation, also keep in mind that cloning nor fetching from shallow repo is not supported! To really remove all the history you also need to remove all references to previous commits before pruning.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With