我如何用特定的版本克隆git存储库,就像我通常在Mercurial中做的那样:
hg clone -r 3 /path/to/repository
我如何用特定的版本克隆git存储库,就像我通常在Mercurial中做的那样:
hg clone -r 3 /path/to/repository
当前回答
对于单个文件,当提交号已知时,可以使用wget onliner:
wget https://raw.githubusercontent.com/torvalds/linux/896066ee1cf4d653057dac4e952f49c96ad16fa7/README
其他回答
克隆一个git存储库,恰当地说,就是克隆整个存储库:没有办法只选择一个修订版本来克隆。然而,一旦你执行了git克隆,你可以通过执行checkout <rev>来检出一个特定的修订。
我可以使用git clone——config选项来完成这一点,这是我从下面的回答中学到的: https://stackoverflow.com/a/43759576/1330650
我的场景涉及Azure DevOps管道中的稀疏签出,其中我需要使用提交散列(而不是分支名称)克隆一个回购。clone命令不接受提交哈希作为参数。解决方法是设置一个包含refspec的配置变量(-c),因为该refspec可以使用提交哈希而不是分支名称:
git clone -c remote.origin.fetch=+<commit hash>:refs/remotes/origin/<commit hash> <repo_url> --no-checkout --progress --depth 1
git sparse-checkout init --cone
git sparse-checkout set <file list>
git checkout <commit hash>
# 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 --depth=1 --branch NAME https://github.com/your/repo.git
不幸的是,NAME只能是分支名称或标记名称(不能提交SHA)。
省略——depth标志来下载整个历史记录,然后签出分支或标记:
git clone --branch NAME https://github.com/your/repo.git
这适用于最新版本的git(我使用2.18.0版本)。