我如何用特定的版本克隆git存储库,就像我通常在Mercurial中做的那样:

hg clone -r 3 /path/to/repository

当前回答

# clone special tag/branch without history
git clone  --branch=<tag/branch> --depth=1 <repository>


# clone special revision with minimal histories
git clone --branch <branch> <repository> --shallow-since=yyyy-MM-ddTHH:mm:ss  # get the commit time
cd <dir>
git reset --hard <revision> 

如果不设置uploadpack,就不能获得没有历史记录的修订。在服务器端allowReachableSHA1InWant=true,而您可以为它创建一个标记并克隆特殊标记。

其他回答

你可以简单地使用git checkout <commit hash>

在这个序列中

bash git克隆[URLTORepository] Git checkout [commit]

提交哈希看起来像这样“45ef55ac20ce2389c9180658fdba35f4a663d204”

git clone -o <sha1-of-the-commit> <repository-url> <local-dir-name>

Git使用了origin这个词,而不是众所周知的revision

以下是手册$ git帮助克隆的一个片段

--origin <name>, -o <name>
    Instead of using the remote name origin to keep track of the upstream repository, use <name>.

git克隆https://github.com/ORGANIZATION/repository.git(克隆存储库)

CD存储库(导航到存储库)

Git获取原点2600f4f928773d79164964137d514b85400b09b2

git checkout FETCH_HEAD

我的版本是公认的答案和最多赞的答案的结合。但它有点不同,因为每个人都使用SHA1,但没有人告诉你如何获得它

$ git init
$ git remote add <remote_url>
$ git fetch --all

现在你可以看到所有的分支&提交

$ git branch -a
$ git log remotes/origin/master <-- or any other branch

最后,您知道了所需提交的SHA1

git reset --hard <sha1>

克隆一个git存储库,恰当地说,就是克隆整个存储库:没有办法只选择一个修订版本来克隆。然而,一旦你执行了git克隆,你可以通过执行checkout <rev>来检出一个特定的修订。