我如何用特定的版本克隆git存储库,就像我通常在Mercurial中做的那样:
hg clone -r 3 /path/to/repository
我如何用特定的版本克隆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 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>
如果你的意思是你想要从开始到某一点的所有东西,查尔斯·贝利的答案是完美的。如果你想做相反的事情,并从当前日期检索历史的子集,你可以使用git clone——depth [N],其中N是你想要的历史的转速。然而:
——深度 创建一个浅克隆,其历史记录被截断为指定的修订数。浅存储库有许多限制(您不能从它复制或获取,也不能从它推入或进入它),但如果您只对具有较长历史的大型项目的最近历史感兴趣,并且希望以补丁的形式发送修复,那么浅存储库就足够了。
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>.
对于单个文件,当提交号已知时,可以使用wget onliner:
wget https://raw.githubusercontent.com/torvalds/linux/896066ee1cf4d653057dac4e952f49c96ad16fa7/README
从Git 2.5.0开始,下面描述的功能可以在服务器端通过配置变量uploadpack启用。allowReachableSHA1InWant,这里是GitHub特性请求和GitHub提交启用该特性。注意,一些Git服务器默认激活这个选项,例如Bitbucket Server从5.5+版本开始启用它。有关如何激活配置选项的示例,请参阅Stackexchange上的回答。
更新1对于Git版本1.7 < v < 2.5使用Git克隆和Git重置,如Vaibhav Bajpai的回答所述
如果你不想获取完整的存储库,那么你可能不应该使用clone。你可以使用fetch来选择你想要获取的分支。我不是hg专家,所以我不知道-r的细节,但在git中你可以做这样的事情。
# make a new blank repository in the current directory
git init
# add a remote
git remote add origin url://to/source/repository
# fetch a commit (or branch or tag) of interest
# Note: the full history up to this commit will be retrieved unless
# you limit it with '--depth=...' or '--shallow-since=...'
git fetch origin <sha1-of-commit-of-interest>
# reset this repository's master branch to the commit of interest
git reset --hard FETCH_HEAD