5

Is there a way to make git pull keep depth=1, just use the latest commit, instead of the one that git clone was made (if there is newer one)?

I I do git clone -b some_branch --depth=1 git_repo.git

It clones the repository with minimum space usage because it removes all the history. Now if I need to update that repository again and use git pull, it pulls the whole history.

There is a similar question here:

Pull updates with git after cloned with --depth 1

If try accepted answer advice and use git pull --unshallow and then git pull --depth=1, it looks like it does not reduce space as git clone --depth=1 does.

So the only way to really reduce repository size is to just remove repository and clone with depth=1 again?. Looks kind of clunky way to do it.

And the reason I need this is that there are repositories used when fully cloned, currently take about ~3 GB in size. And there are like 40 environments where it is used. So in total, it uses a lot of space. With a shallow clone, it can be reduced about 5 times.

Sample:

Cloning this repository branch 12.0 git@github.com:odoo/odoo.git, shows its size to be around 3 GB.

Cloning this repository branch 12.0 with depth=1, shows size to be 643 MB.

Using --unshallow on pull and then (as suggested here Converting git repository to shallow?):

git pull --depth 1
git gc --prune=all

Does not seem to shrink size as shallow clone does.

Andrius
  • 19,658
  • 37
  • 143
  • 243
  • Possible duplicate of [Converting git repository to shallow?](https://stackoverflow.com/questions/4698759/converting-git-repository-to-shallow) – EncryptedWatermelon Oct 24 '19 at 10:20
  • @EncryptedWatermelon Most upvoted answer seems to not work in all cases. I tried pruning biggest repository and it did not shrink in size at all. Though, another repository that is smaller in size, did shrink. I also tried suggestions in comments, but that did not change anything. – Andrius Oct 24 '19 at 13:04
  • @Andrius: read *all* the comments on [this answer](https://stackoverflow.com/a/40452701/1256452). Also, see [VonC's answer there](https://stackoverflow.com/a/53245223/1256452) and make sure your Git version is up-to-date enough. – torek Oct 24 '19 at 16:13

1 Answers1

2

Unfortunately, the answer is "no". Depth places commits in the .git/shallow file. When retrieving commit history, your request will stop at the commits in the shallow file, but if there were a merge into the current branch, it will follow that and the whole history behind it. From my blog post, Exploring Git Clone --depth:

If you had a branch structure that you did a git clone --depth=1 when main was at c:

...  -  .  -  .  - [c] -  .  -  .  -  .  -  .  (main)
         \               /
           .  -  .  -  .  (xyz)

And then later did a fetch at g, the merge at d would cause you to pull nearly the whole history (except b).

1000’s of commits  -  a  -  .  - [c] -  d  -  e  -  f  -  g  (main)
                       \               /
                         x  -  y  -  z  (xyz)

The above medium blog post gives some suggestions, but no answer to your question.