Reputation: 10901
Git allows to fetch from any given remote and reference, for example
git fetch <remote-url> <reference>
So that those given commits are available without having to add remotes or creating branches.
This however only works for references, like branch names or tags, but not for specific hashes and thus commits that are not referenced directly anywhere.
Is there a way to fetch a specific commit from a remote?
Upvotes: 52
Views: 32747
Reputation: 1323045
See "Pull a specific commit from a remote git repository":
With Git 2.5 (July 2015), you will be able to do:
git fetch --depth=1 <a/remote/repo.git> <full-length SHA1>
git cat-file commit $SHA1
If the SHA1 is "reachable" from one of the branch tips of the remote repo, then you can fetch it.
Caveats:
uploadpack.allowReachableSHA1InWant
config (and you need that config to be set to true
, in order to allow a single commit fetch).git rev-parse
, since you don't have all the commits, as noted by Jim Hurne in the comments.Upvotes: 19
Reputation: 109
git fetch [remote] [sha] && git checkout FETCH_HEAD
will allow you to checkout a commit that is on the remote; even if that sha is not yet merged (like in a Gerrit environment).
or... git fetch [remote] [sha] && git cherry-pick FETCH_HEAD
if you wish to cherry-pick it instead.
Examples:
git fetch origin d8a9a9395bb0532a0498f800e38ef5e60dfb173f && git checkout FETCH_HEAD
git fetch origin d8a9a9395bb0532a0498f800e38ef5e60dfb173f && git cherry-pick FETCH_HEAD
Upvotes: 2
Reputation: 9263
As today I tried:
git fetch origin <commit-hash>
And it works like a charm! (git version 2.20.1)
Just be sure the <commit-hash>
is the full length reference
Upvotes: 62
Reputation: 363467
No. According to the manual, git fetch
wants a refspec, the simplest form of which is a ref, and a bare SHA-1 isn't a ref. I.e., the commit has to have a name (branch, tag) on the remote for you to be able to fetch
it.
Upvotes: 10