如何克隆具有特定修订版/变更集的 git 存储库? [英] How to clone git repository with specific revision/changeset?
问题描述
如何克隆具有特定修订版本的 git 存储库,就像我通常在 Mercurial 中所做的那样:
How can I clone git repository with specific revision, something like I usually do in Mercurial:
hg clone -r 3 /path/to/repository
推荐答案
UPDATE 2 自 Git 2.5.0 下面描述的功能可以在服务器端使用配置变量uploadpack.allowReachableSHA1InWant
strong>,这里是 GitHub 功能请求 和 GitHub 提交启用此功能.请注意,某些 Git 服务器默认激活此选项,例如Bitbucket Server 从 5.5+ 版 开始启用它.有关如何激活配置选项的示例,请参阅Stackexchange 上的答案.
UPDATE 2 Since Git 2.5.0 the feature described below can be enabled on server side with configuration variable uploadpack.allowReachableSHA1InWant
, here the GitHub feature request and the GitHub commit enabling this feature. Note that some Git servers activate this option by default, e.g. Bitbucket Server enabled it since version 5.5+. See this answer on Stackexchange for a exmple of how to activate the configuration option.
更新 1 对于 Git 版本 1.7 <<2.5
使用 git clone 和 git reset,如 Vaibhav Bajpai 的回答
UPDATE 1 For Git versions 1.7 < v < 2.5
use git clone and git reset, as described in Vaibhav Bajpai's answer
如果您不想获取完整的存储库,那么您可能不应该使用 clone
.你总是可以使用 fetch 来选择你想要获取的分支.我不是 hg 专家,所以我不知道 -r
的细节,但在 git 中你可以做这样的事情.
If you don't want to fetch the full repository then you probably shouldn't be using clone
. You can always just use fetch to choose the branch that you want to fetch. I'm not an hg expert so I don't know the details of -r
but in git you can do something like this.
# 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
这篇关于如何克隆具有特定修订版/变更集的 git 存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!