我如何用特定的版本克隆git存储库,就像我通常在Mercurial中做的那样:
hg clone -r 3 /path/to/repository
我如何用特定的版本克隆git存储库,就像我通常在Mercurial中做的那样:
hg clone -r 3 /path/to/repository
当前回答
如果你的意思是你想要从开始到某一点的所有东西,查尔斯·贝利的答案是完美的。如果你想做相反的事情,并从当前日期检索历史的子集,你可以使用git clone——depth [N],其中N是你想要的历史的转速。然而:
——深度 创建一个浅克隆,其历史记录被截断为指定的修订数。浅存储库有许多限制(您不能从它复制或获取,也不能从它推入或进入它),但如果您只对具有较长历史的大型项目的最近历史感兴趣,并且希望以补丁的形式发送修复,那么浅存储库就足够了。
其他回答
对于单个文件,当提交号已知时,可以使用wget onliner:
wget https://raw.githubusercontent.com/torvalds/linux/896066ee1cf4d653057dac4e952f49c96ad16fa7/README
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
希望这个解决方案能继续工作几年!: -)
$ 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>.
你可以简单地使用git checkout <commit hash>
在这个序列中
bash git克隆[URLTORepository] Git checkout [commit]
提交哈希看起来像这样“45ef55ac20ce2389c9180658fdba35f4a663d204”