How to maintain shallow clone of a set of branches

2019-06-25 00:15发布

问题:

I'd like to maintain a shallow, mirrored, bare clone of several branches. I will clone locally from that for different project branches. e.g.

+------------------------------------------------------------------+
| repo1: server1:original-repo.git branches: A, B, C, D, E         |
+------------------------------------------------------------------+
  ↓
+------------------------------------------------------------------+
| repo2: server2:shallow-bare-selective-clone branches: A, B only  |
+------------------------------------------------------------------+
  ↓                                 ↓
+-------------------------------+ +--------------------------------+
| repo3: server2:clone repo2, A | | repo4: server2:clone repo2, B  |
+-------------------------------+ +--------------------------------+

So I can make repo2 like this

git clone --bare --mirror --depth 1 server1:repo1  repo2
cd repo2
git fetch --depth 200 origin A
git fetch --depth 200 origin B

And then I think repo3 and 4 are really easy - they can clone all they want from repo2, they will be limited by the shallowness of repo2.

But keeping repo2 up to date from repo1 while maintaining its shallowness is what I'm stuck on (ideally I'd like to maintain everything since a certain commit, but I understand that's not possible). It seems I have to do the multiple git fetch commands each time, is this right? Is there a way to pop that in the config file so that I can just do git fetch and it knows what I mean?

回答1:

I faced this question when searching for analogous sophisticated Git needs and perfectly understand that question is 2 years old. I'm answering because there are no answers at all whereas question is still very valid.

Shallowness is primarily fetch property. So it's possible to do fetch with --depth 200 and maintain history without too old commits.

Modern versions of Git have --shallow-exclude option. I think it's exactly what allows to "maintain everything since a certain commit".