4

Problem and Motivation

We have a larger git repository on our central server. There is a lot of history in it that we want to keep, so rebasing or squashing anything there is not really an option. However, when cloning the repository to our development servers, only recent history is actually relevant. For us, "recent" can be defined as a given tag and any commit earlier in the history can be omitted. The aim here is to save bandwidth, time and disk space.

Ideas for Approach

The current idea is to clone that tag only, using git clone --branch my-root-tag --depth 1, treating it as an artificial root commit in that local repository. Afterwards, adding branches that we'd like to fetch manually using git remote set-branches --add origin some-branch. All of these branches must include my-root-tag somewhere in their history. However, every fetch would now again transfer the entire history. Is there any way to restrict fetches to stop at my-root-tag instead? It sounds like this approach would need something like asked in git shallow clone since specific commit, ideally wrapped in a git alias to "dynamically" calculate the value of the --depth parameter.

Can anyone think of a way to get this to work or even by applying some entirely different approach?

Results

edit: To summarize, it seems that a git fetch does indeed only transfer the commits up to the grafted new repository root (at least in Git v 2.25.1). Only the original clone needs to be parameterized correctly, while subsequent operations on the repository can be performed using regular (unparameterized) git commands. No shallow-include or shallow-exclude options are actually necessary now. This is great news as it makes that configuration a lot less fragile than initially feared.

Michael Lang
  • 3,902
  • 1
  • 23
  • 37
  • Related: https://stackoverflow.com/questions/33612627. You did not mention `--shallow-since`, mentioned in the question you linked. Is that not working for you? – tkruse Apr 01 '20 at 14:25
  • Maybe also explain why `git fetch shallow-exclude=my-root-tag` would not work for you. – tkruse Apr 01 '20 at 15:01
  • The `--shallow-since` and especially `--shallow-exclude` parameters do indeed work almost like needed for that use case. However, I have not managed to connect the pulled branches to my `root-tag`. Even when I created another tag one or two commits before the root and using that one to pull. – Michael Lang Apr 06 '20 at 07:40
  • I am not sure what you mean by "connect pulled branches to tag". Maybe it would help if you could draw the situation as ASCII art of paste git glog or similar. See https://stackoverflow.com/questions/1838873. You might need to play around a bit, but it's difficult to help with your question without a clear understanding of the git branching on your remote and on the clones. – tkruse Apr 06 '20 at 08:00
  • What I meant was, that `git branch -a --contains my-root-tag` yielded no branches. The root commit for my added branches was a newer one than then one passed as parameter to `--shallow-exclude`. However, a `clone --depth 1` followed by a regular `fetch` just seemed to have worked as desired. I already thank you for your support, @tkruse, conduct some more tests today and close that question afterwards. – Michael Lang Apr 06 '20 at 09:00
  • Cool. Also not all fetches fetch tags, not sure if that impacts you: https://stackoverflow.com/questions/15794378 – tkruse Apr 06 '20 at 10:21

1 Answers1

3

It seems that fetching everything up to a commit is what should happen using --shallow-exclude=my-root-tag See How to maintain shallow clone of a set of branches in git

This similar question suggests using --shallow-since=<date of tagged commit>: Git: Shallow copies from a specific commit

Also related:

tkruse
  • 10,222
  • 7
  • 53
  • 80