我如何用特定的版本克隆git存储库,就像我通常在Mercurial中做的那样:
hg clone -r 3 /path/to/repository
我如何用特定的版本克隆git存储库,就像我通常在Mercurial中做的那样:
hg clone -r 3 /path/to/repository
当前回答
mkdir linux-4.3.20151106
cd linux-4.3.20151106/
git init
git fetch git@github.com:torvalds/linux.git 9154301a47b33bdc273d8254c407792524367558
error: unknown option `no-write-fetch-head'
usage: git fetch [<options>] [<repository> [<refspec>...]] or: git fetch [<options>] <group>
or: git fetch --multiple [<options>] [(<repository> | <group>)...] or: git fetch --all [<options>]
git --version
git version 2.17.1
export https_proxy=http://192.168.1.3:1080;export http_proxy=http://192.168.1.3:1080
add-apt-repository ppa:git-core/ppa
apt update
apt-get install --only-upgrade git
git --version
git version 2.38.0
git fetch git@github.com:torvalds/linux.git 9154301a47b33bdc273d8254c407792524367558 --no-write-fetch-head --depth=1
remote: Enumerating objects: 54692, done.
remote: Counting objects: 100% (54692/54692), done.
remote: Compressing objects: 100% (50960/50960), done.
remote: Total 54692 (delta 3828), reused 29210 (delta 2966), pack-reused 0
Receiving objects: 100% (54692/54692), 147.35 MiB | 2.85 MiB/s, done.
Resolving deltas: 100% (3828/3828), done.
git branch master 9154301a47b33bdc273d8254c407792524367558
git checkout
其他回答
总结一下(git v. 1.7.2.1):
在你想要回购的地方做一个常规的git克隆(得到所有的日期-我知道,不是想要的,我们正在那里) Git checkout <sha1 rev>你想要的rev Git重置——很难 Git checkout -b master
克隆单个分支的完整工作流程,选择提交,然后检查特定的提交…此方法需要git版本2.28.0或更高的版本才能使用选项——no-write-fetch-head,使用版本2.35.3进行测试。(如果你已经知道你想要提交的完整sha1哈希值,请跳过最后一个代码块中的第二个方法)
#Create empty git repo
mkdir repo && cd repo && git init
#add remote, configure it to track <branch>
git remote add --no-tags -t <branch> -m <branch> origin <url>
#fetch objects from remote repo
git fetch --no-write-fetch-head
#examine commits and logs to decide which one we will use
git log --oneline origin
#Once you have found the commit of interest copy the abbreviated hash or save as variable
commit=<sha1>
#rename our default branch to match remote branch
git branch -m <branch>
#set branch head to desired commit
git branch <branch> $commit
#set remote branch as upstream for <branch>
git branch -u origin <branch>
#All done time to checkout
git checkout
可以选择截断本地分支的历史记录:
git fetch --no-write-fetch-head --depth <n> ./ <branch>
要截断远程分支历史记录,可以执行以下命令,但请记住,如果你将历史记录截断为一个比你签出的提交更新的提交,git状态会告诉你,你已经与远程分支分离了<n>次提交
git fetch --no-write-fetch-head --depth <n>
如果你不需要远程跟踪,并且已经知道完整的提交散列:
mkdir repo && cd repo && git init
git remote --no-tags add origin <url>
git fetch --depth 1 --no-write-fetch-head origin <sha1>
#Set default local branch (master in this case) head to <sha1>
git branch master <sha1>
git checkout
在我看来,这个方法更好的地方在于它真正只获取一个提交。我们还避免创建FETCH_HEAD或ORIG_HEAD,使.git目录保持干净。这也使得reflog干净(只有一个条目),而不是由于git重置而有两个条目——硬提交 不需要远程跟踪和使用fetch—depth 1,它创建尽可能小的克隆(浅克隆)。
如果你的意思是你想要从开始到某一点的所有东西,查尔斯·贝利的答案是完美的。如果你想做相反的事情,并从当前日期检索历史的子集,你可以使用git clone——depth [N],其中N是你想要的历史的转速。然而:
——深度 创建一个浅克隆,其历史记录被截断为指定的修订数。浅存储库有许多限制(您不能从它复制或获取,也不能从它推入或进入它),但如果您只对具有较长历史的大型项目的最近历史感兴趣,并且希望以补丁的形式发送修复,那么浅存储库就足够了。
我可以使用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 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