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

hg clone -r 3 /path/to/repository

当前回答

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>.

其他回答

DR -只需在源存储库中针对您想要克隆到的提交创建一个标记,并在fetch命令中使用该标记。您可以稍后从原始回购中删除标记以进行清理。

好吧,现在已经是2014年了,看起来查尔斯·贝利2010年的答案已经过时了,而且大多数(所有?)其他答案都涉及克隆,许多人都希望避免克隆。

下面的解决方案实现了OP和其他许多人正在寻找的东西,这是一种创建存储库副本(包括历史记录)的方法,但仅限于特定的提交。

以下是我在git 2.1.2版本中使用的命令,用于克隆一个本地回购(即。在另一个目录中的存储库)到某一点:

# in the source repository, create a tag against the commit you want to check out
git tag -m "Temporary tag" tmptag <sha1>

# create a new directory and change into that directory
cd somewhere_else;mkdir newdir;cd newdir

# ...and create a new repository
git init

# add the source repository as a remote (this can be a URL or a directory)
git remote add origin /path/to/original/repo

# fetch the tag, which will include the entire repo and history up to that point
git fetch origin refs/tags/tmptag

# reset the head of the repository
git reset --hard FETCH_HEAD

# you can now change back to the original repository and remove the temporary tag
cd original_repo
git tag -d tmptag

希望这个解决方案能继续工作几年!: -)

我使用这个代码片段和GNU make来关闭任何修订标记、分支或散列

这是在 Git 2.17.1版本

${dir}:
    mkdir -p ${@D}
    git clone --recursive --depth 1 --branch ${revison} ${url} ${@} \
 || git clone --recursive --branch ${revison} ${url} ${@} \
 || git clone ${url} ${@}
    cd ${@} && git reset --hard ${revison}
    ls $@




如果你的意思是你想要从开始到某一点的所有东西,查尔斯·贝利的答案是完美的。如果你想做相反的事情,并从当前日期检索历史的子集,你可以使用git clone——depth [N],其中N是你想要的历史的转速。然而:

——深度 创建一个浅克隆,其历史记录被截断为指定的修订数。浅存储库有许多限制(您不能从它复制或获取,也不能从它推入或进入它),但如果您只对具有较长历史的大型项目的最近历史感兴趣,并且希望以补丁的形式发送修复,那么浅存储库就足够了。

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

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

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

Git获取原点2600f4f928773d79164964137d514b85400b09b2

git checkout FETCH_HEAD