1

I have a shallow clone that I need to push to a remote repository (the remote repository does not contain any of the history from that branch). When I try to push it, it gives me an error about "Shallow update not allowed".

I tried to do it on a "local" remote repository I just created and I (of course) get the same error. I did find OTOH that I can eliminate the error if I first copy the .git/shallow to the destination repository. But my "real" remote repository is one to which I do not have shell access, so I cannot just scp the file to it. How can I convince the remote Git to create this .git/shallow file for me?

Stefan
  • 27,908
  • 4
  • 53
  • 82

2 Answers2

2

It depends on the remote server, but you also have, on that remote server, the option to set in the target bare repository the receive.shallowUpdate option:

git config --local --add receive.shallowUpdate true

And locally, you can try first a git fetch --update-shallow to accept refs that require updating .git/shallow.

If you cannot do those two option (one remote, one local), then you need to git fetch --unshallow and work from there.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • That first option would require direct access to the repository, though, which the OP already excluded. The second one is a good idea (though it's less likely to succeed than a full unshallow). – torek Sep 01 '22 at 08:09
  • @torek Agreed. Again, it depends on the nature of the remote Git repository hosting service, and the right of the user with that service. If you can defines hooks, for instance, then you can change through a hook the local Git config of a repository. No shell access required. – VonC Sep 01 '22 at 09:55
  • The goal is to have a `shallow` in the remote (central) server, so users can work with a 20MB .git directory rather than a 1GB one that includes (now) irrelevant blobs. So "unshallowing" defeats the purpose. – Stefan Sep 02 '22 at 12:36
  • But thanks for mentioning `receive.shallowUpdate` of which I was not aware. – Stefan Sep 02 '22 at 12:59
  • @Stefan Yes, can you set `receive.shallowUpdate` on the server? (through a hook script for instance). What is your remote server? – VonC Sep 02 '22 at 14:44
  • Yes, I think I can get someone else to set that var for me. Thanks. – Stefan Sep 03 '22 at 13:52
-1

You don't. A shallow clone is made at git clone time, by creating a .git/shallow file with the right contents (commit hash IDs). The git push command is unable to create or update this file; only git fetch can update it, or remove it entirely, when changing the depth of, or "un-shallowing", a clone (with --depth, --deepen, or --unshallow). The git init command that creates a new, empty repository never creates this file; only git clone (which runs git fetch internally) and git fetch can create or update it.

Your best bet is to find or create some full clone, and use that to push to the remote repository to which you have git push access, then git push your new commits from the existing shallow repository. If the full clone is very large (occupies too much space on your laptop for instance), you can remove it after copying it this way.

Side note: Git does not use the phrase shallow branch, only shallow clone or shallow repository. That's because the word branch, whatever you mean by it—different people mean different things at different times—doesn't really fit well here.

torek
  • 448,244
  • 59
  • 642
  • 775
  • The problem is not how to push my branch to the remote, but how to create a `shallow` on the remote. – Stefan Sep 02 '22 at 12:41
  • Unless there's some option to your "create new repository" operation as provided *by* the remote (or you have direct shell access), I stand by my answer that you don't. – torek Sep 02 '22 at 12:43