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

hg clone -r 3 /path/to/repository

当前回答

$ git clone $URL
$ cd $PROJECT_NAME
$ git reset --hard $SHA1

再次返回到最近的提交

$ git pull

为了在线(远程)保存还原的提交,你必须强制执行origin:

git push origin -f

其他回答

# 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 clone $URL
$ cd $PROJECT_NAME
$ git reset --hard $SHA1

再次返回到最近的提交

$ git pull

为了在线(远程)保存还原的提交,你必须强制执行origin:

git push origin -f
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>.

我的版本是公认的答案和最多赞的答案的结合。但它有点不同,因为每个人都使用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>

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

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