Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting git repository to shallow?

Tags:

git

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.

like image 998
Robert Avatar asked Jan 15 '11 08:01

Robert


People also ask

What is a shallow git repository?

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).

What is shallow commit?

Definition. Shallow commits do have parents, but not in the shallow repo, and therefore grafts are introduced pretending that these commits have no parents.

What is shallow cloning and deep cloning in git?

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.


2 Answers

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.

like image 80
fuzzyTew Avatar answered Oct 17 '22 19:10

fuzzyTew


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.

like image 22
user212328 Avatar answered Oct 17 '22 21:10

user212328