Git fetch a specific commit by hash

Git fetch a specific commit by hash

回答1

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

 

回答2

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-lenght 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:

  • you need a Git 2.5 remote repo server though, which will handle the uploadpack.allowReachableSHA1InWant config (and you need that config to be set to true, in order to allow a single commit fetch).
  • And, as illustrated in crgarridos's answer, you need the full SHA1, and you cannot use git rev-parse, since you don't have all the commits, as noted by Jim Hurne in the comments.

 

git config --global uploadpack.allowReachableSHA1InWant true

 

git fetch origin 4e16940d61d581daf7db90fd67e1e45a107c5139  这里到commit id必须是完整到,否则到话,不能正常识别。

 

posted @ 2022-08-10 16:01  ChuckLu  阅读(71)  评论(0)    收藏  举报