Can I rely on `git fetch <sha>`?

Viewed 47

I am trying to build a workflow which syncs to a remote git repo, with as little overhead as possible. Some of the repos I want to sync are quite large and I don't need the history (--depth 1). The real issue is that I want to sync to any of: a branch, a tag, a SHA.

I can't clone at a SHA, as far as I can tell.

Then I read this wonderful comment: Retrieve specific commit from a remote Git repository

It suggests that git fetch <remote> <sha> works. Empirically it works on file:// repos and on GitHub repos. My question is really: can I rely on this? Or are there reasons that major providers might disable this?

1 Answers

You can rely on this if the provider uses the default Git implementation (that is, the canonical one written in C) and they support protocol v2 (protocol.version=2) because the default implementation doesn't do any checks in that case. GitHub offers that, and some other providers do as well.

However, other providers may use a different implementation (e.g., Google and Gerrit use JGit, I believe) and not all providers support protocol version 2, so it isn't guaranteed that all providers will support it.

The usual case for not supporting it is that the provider doesn't want people pulling objects that are no longer referenced. Some providers also share objects between forks and don't want objects available from one fork accessible from another. Fetching arbitrary commits would let folks access data that was no longer accessible or that was from a private fork.

Related