Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compact Git repository with history depth

Tags:

git

In Git, there is a command git clone --depth=<depth> to retrieve historical data only specific length. there are also a command to gather more historical data by use git fetch --depth=<depth>.

How about when we want to free some spaces from large repository? I know that we may use git gc or git prune but are there other way to specific like --depth=<depth> to reduce number of commit store in local repository? AND it also should keep SHA1 to be able continue to working with it.

like image 685
scalopus Avatar asked Oct 31 '22 22:10

scalopus


1 Answers

The easiest way would be to:

  • delete entirely the current local repo
  • git clone --depth=n /url/of/remote/repo

That would clone the last n commits, while allowing fetch/pull/psuh to still work with the remote repo.

since Git 2.5, you can fetch a single commit, but unless that commit is the latest one (which is like a git clone --depth=1), that would not allo for fetch/pull/push.

The other approach to make sure a given local repo is as lean as possible is to use a combination of gc/prune/repack:

git gc --aggressive
git repack -Ad       # kills in-pack garbage
git prune --progress # kills loose garbage
like image 68
VonC Avatar answered Nov 13 '22 07:11

VonC