How to clone git repository with specific revision/changeset?
How to clone git repository with specific revision/changeset?
回答1
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.
UPDATE 1 For Git versions 1.7 < v < 2.5
use git clone and git reset, as described in Vaibhav Bajpai's answer
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
回答2
No need to download the whole history, and no need to call git init
:
git clone --depth=1 URL
git fetch --depth=1 origin SHA1
git checkout SHA1
git branch -D @{-1} # if you want to tidy up the fetched branch
This has the disadvantage, to CB Baileys answer, that you will still download 1 unnecessary revision. But it's technically a git clone
(which the OP wants), and it does not force you to download the whole history of some branch.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2022-09-12 一道微分方程应用题中的“微”妙解答 高等数学
2021-09-12 详讲口诀“奇变偶不变,符号看象限”
2019-09-12 Forms Authentication and Role based Authorization: A Quicker, Simpler, and Correct Approach
2019-09-12 How does Request.IsAuthenticated work?
2019-09-12 细说ASP.NET Forms身份认证
2019-09-12 IIS URL Rewriting and ASP.NET Routing
2019-09-12 Razor syntax reference for ASP.NET Core