git 如何浅克隆深度为 1 的特定提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31278902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:22:23  来源:igfitidea点击:

How to shallow clone a specific commit with depth 1?

gitshallow-clone

提问by Chin

Is it possible to shallow clone a specific commit in a repository, i.e. with depth 1? Something like

是否可以浅克隆存储库中的特定提交,即深度为 1?就像是

git clone http://myrepo.git 728a4d --depth 1

to get the repository state as it is at the commit with SHA 728a4d...?

获取存储库状态,因为它在提交时使用 SHA 728a4d...

The motivation is to avoid having to clone the whole repository, then check out that specific commit, when we're only interested in the state of the repository at that specific commit.

动机是避免克隆整个存储库,然后检查该特定提交,当我们只对特定提交的存储库状态感兴趣时。

采纳答案by sschuberth

Starting with Git 2.5.0 (which needs to be available at boththe client and server side) you can set uploadpack.allowReachableSHA1InWant=trueon the server side to enable fetching of specific SHA1s:

从 Git 2.5.0(需要在客户端和服务器端可用)开始,您可以uploadpack.allowReachableSHA1InWant=true在服务器端设置以启用特定 SHA1 的获取:

git init
git remote add origin <url>
git fetch --depth 1 origin <sha1>
git checkout FETCH_HEAD

Note that I did not find a syntax to do this with git clonedirectly.

请注意,我没有找到git clone直接执行此操作的语法。

回答by Kirby

NOTE: My example doesn't help to clone to by a commit hash but it will help to clone a tag and have a lightweight repository.

注意:我的示例无助于通过提交哈希克隆到,但它有助于克隆标签并拥有轻量级存储库。

If you have to have only one commit in your "clone" and you are going to use commit hash, short answer isNO.

如果您的“克隆”中只有一个提交,并且您打算使用提交哈希,那么简短的回答是NO

I use this command construction (tested on v2.13.2.windows.1) for tags:

我使用这个命令构造(在v2.13.2.windows.1测试)作为标签:

git clone --depth 1 [email protected]:VENDOR/REPO.git --branch 1.23.0 --single-branch

Full example:

完整示例:

$ git clone --depth 1 [email protected]:Seldaek/monolog.git --branch 1.23.0 --single-branch
Cloning into 'monolog'...
remote: Counting objects: 201, done.
remote: Compressing objects: 100% (188/188), done.
remote: Total 201 (delta 42), reused 32 (delta 5), pack-reused 0
Receiving objects: 100% (201/201), 190.30 KiB | 0 bytes/s, done.
Resolving deltas: 100% (42/42), done.
Note: checking out 'fd8c787753b3a2ad11bc60c063cff1358a32a3b4'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

$ cd monolog

.gitdir size (267Kvs 2.6Mby using full clone):

.git目录大小(使用 full 为267K2.6Mclone):

$ du -h --max-depth=0 .git
267K    .git

I'd like to denote, --branchcan take a tag/branch.

我想表示,--branch可以带一个标签/分支。

https://git-scm.com/docs/git-clone#git-clone---branchltnamegt

https://git-scm.com/docs/git-clone#git-clone---branchltnamegt

--branchcan also take tags and detaches the HEAD at that commit in the resulting repository.

--branch还可以在结果存储库中的该提交中获取标签并分离 HEAD。

UPD

UPD

In a nutshell, it can take "refs". You may read more here: What does the git error message “Server does not allow request for unadvertised object” mean?

简而言之,它可以接受“refs”。您可以在此处阅读更多信息:git 错误消息“服务器不允许请求未通告的对象”是什么意思?

Also, there is notricks like:

此外,没有像这样的技巧:

git fetch --depth 1 origin <COMMIT_HASH>

Thanks @BenjiWiebe for pointing me in my mistake.

感谢@BenjiWiebe 指出我的错误。

回答by CodeWizard

The immediate answer is: You cant.
Why? detailed explain can be found here: Why Isn't There A Git Clone Specific Commit Option?

直接的答案是:你不能。
为什么?详细解释可以在这里找到:为什么没有 Git 克隆特定提交选项?

What else can you do?

你还能做什么?

How to clone repository to a specific commit? (full clone)

如何将存储库克隆到特定提交?(完整克隆)

# Create empty repository to store your content
git clone <url>
git reset <sha-1> --hard


More info:

更多信息:

How to clone single branch?

如何克隆单个分支?

git clone <url> --branch <branch_name> --single-branch <folder_name>

git clone <url> --branch <branch_name> --single-branch <folder_name>

How to clone only latest commit from a given branch?

如何仅从给定分支克隆最新提交?

git clone <url> --depth=1 --branch <branch_name> --single-branch <folder_name>

git clone <url> --depth=1 --branch <branch_name> --single-branch <folder_name>

How to shallow clone a specific commit with depth 1?

如何浅克隆深度为 1 的特定提交?

As @sschuberth commented out: --depthimplies --single-branch.

正如@sschuberth 所评论的:--depth暗示--single-branch.

Instead of clone use the fetch command:

使用 fetch 命令代替克隆:

# fetch a commit (or branch or tag) of interest
# In this case you will have the full history of this commit
git fetch origin <sha1>

回答by Corin

Try using whilein bash:

尝试while在 bash 中使用:

git clone --depth=1 $url
i=1; while ! git show $sha1; do git fetch --depth=$((i+=1)); done

This is pretty slow because it fetches each commit individually; you could increase the increment (to fetch commits in batches and improve performance over a network) but it's still a brute force approach.

这很慢,因为它单独获取每个提交;您可以增加增量(以批量获取提交并提高网络性能),但这仍然是一种蛮力方法。