如何从存储库下载特定的 git commit?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3697707/
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
How do I download a specific git commit from a repository?
提问by bigredbob
I don't have a local code copy/etc, I just want to download a single specific git commit so I can view it. I have the url for the git repository:
我没有本地代码副本/等,我只想下载一个特定的 git commit 以便我可以查看它。我有 git 存储库的 URL:
git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6.git
git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6.git
and the commit hash:
和提交哈希:
ee9c5cfad29c8a13199962614b9b16f1c4137ac9
How can I download just this commit using git (I don't want the whole repo, just the one commit patch)? I have read the man pages for git-pull and git-cherry-pick and fiddled with the commands with no luck.
如何使用 git 仅下载此提交(我不想要整个 repo,只需要一个提交补丁)?我已经阅读了 git-pull 和 git-cherry-pick 的手册页,并在没有运气的情况下摆弄了命令。
Cloning the repo really isn't an option because some of the Kernel repositories are exceedingly large and slow to download (hours).
克隆存储库确实不是一种选择,因为某些内核存储库非常大且下载速度很慢(数小时)。
回答by Josh Lee
This would appear to be impossible. According to a discussionon kernel.org, the protocol will only allow named refs to be fetched. If you don't wish to download the snapshot from the git website, you'll have to clone the entire repo.
这似乎是不可能的。根据kernel.org上的讨论,该协议将只允许获取命名的 refs。如果您不想从 git 网站下载快照,则必须克隆整个存储库。
(You may wish to read the manuals for git-fetchand git-ls-remote.)
(您可能希望阅读git-fetch和git-ls-remote的手册。)
回答by mipadi
In the general case, you can do this using the --remote
flag to git archive
, like so:
在一般情况下,您可以使用--remote
标志 to来执行此操作git archive
,如下所示:
$ git archive -o repo.tar --remote=<repo url> <commit id>
So in your example, you'd use:
所以在你的例子中,你会使用:
$ git archive -o repo.tar --remote=git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6.git ee9c5cfad29c8a13199962614b9b16f1c4137ac9
That'll give you the state of the repo at that point in time. Note that you won't get the whole repo, so you can't actually interact with the upstream repo with what you've downloaded.
这会给你那个时间点的回购状态。请注意,您不会获得整个存储库,因此您实际上无法通过下载的内容与上游存储库进行交互。
However, using git archive
remotely has to be enabled server-side, and it isn't on the Linux kernel's Git server. You can, however, grab a copy by using a URL of the form http://git.kernel.org/?p=<path to repo>;a=snapshot;h=<commit id>;sf=tgz
. So for your repo, you could use, say, wget or curl to grab the file using that URL.
但是,git archive
远程使用必须在服务器端启用,并且不在 Linux 内核的 Git 服务器上。但是,您可以使用表单的 URL 获取副本http://git.kernel.org/?p=<path to repo>;a=snapshot;h=<commit id>;sf=tgz
。因此,对于您的存储库,您可以使用 wget 或 curl 来使用该 URL 获取文件。
回答by araemo
I know this works when you have a clone of the repo (or a similar repo)..
我知道当你有一个 repo(或类似的 repo)的克隆时,这会起作用。
git fetch git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6.git
git cherry-pick ee9c5cfad29c8a13199962614b9b16f1c4137ac9
Fetch downloads the remote refs (but not the whole repo, I don't think) so that you can reference them with the other commands. Then you cherry-pick that one commit, and it attempts to apply the diff to your local tree.
Fetch 下载远程引用(但不是整个 repo,我不认为),以便您可以使用其他命令引用它们。然后你挑选那个提交,它尝试将差异应用到你的本地树。
I haven't tried it without a git repo locally, but I have tried it on repos that are not forks of eachother. you may get merge conflicts, but you can clean those up manually.
我没有在本地没有 git repo 的情况下尝试过它,但是我已经在不是彼此分叉的 repos 上尝试过它。您可能会遇到合并冲突,但您可以手动清理它们。
回答by Joshua Smith
git show COMMITID
But you have to clone the repo. No way around that, I think. But you can do a shallow clone using the --depth arg.
但是你必须克隆 repo。没办法,我想。但是您可以使用 --depth 参数进行浅层克隆。
Also, found a good SO post that covers this topic in greater depth Browse and display files in a git repo without cloning
此外,找到了一篇很好的 SO 帖子,其中更深入地介绍了该主题Browse and display files in a git repo without cloning
回答by minhas23
I was in the same need. I need to download a patch of a specific commit which I need to apply onto another branch.
我也有同样的需要。我需要下载我需要应用到另一个分支的特定提交的补丁。
I used: git format-patch sha-id
我用了: git format-patch sha-id
Just specify the sha-id
which is just older than the required commit.
It will give .patch
files corresponding to the all commits.
只需指定sha-id
比所需提交更早的那个。它将提供.patch
与所有提交相对应的文件。
回答by Jorri Fransen
I ran in to this problem today, and have found a way to download archives for a specific commit. If you go to a repository on git.kernel.org there is a clone section on the bottom. The bottom url is a google git mirror. You will be able do download archives for specific commits from here.
我今天遇到了这个问题,并找到了一种下载特定提交档案的方法。如果您访问 git.kernel.org 上的存储库,则底部有一个克隆部分。底部 url 是一个 google git 镜像。您可以从这里下载特定提交的档案。
回答by David
There is a way to do this:
有一种方法可以做到这一点:
git show ee9c5cfad29c8a13199962614b9b16f1c4137ac9 > myCommittedCode.txt
回答by Daenyth
In this case, if all you want is the diff, you can download it from the web frontend for the kernel repo at this url:
在这种情况下,如果你想要的只是差异,你可以从内核 repo 的 web 前端在这个 url 上下载它:
http://git.kernel.org/?p=linux/kernel/git/davem/net-2.6.git;a=commitdiff_plain;h=ee9c5cfad29c8a13199962614b9b16f1c4137ac9
You can play with the url to get other commits.
您可以使用 url 来获取其他提交。