从存储库中检索单个文件

发布于 2024-07-27 03:12:17 字数 391 浏览 8 评论 0原文

从远程 git 存储库获取单个文件的内容的最有效的机制(就传输的数据和使用的磁盘空间而言)是什么?

到目前为止,我已经设法想出:

git clone --no-checkout --depth 1 [email protected]:foo/bar.git && cd bar && git show HEAD:path/to/file.txt

这似乎仍然有点矫枉过正。

从存储库获取多个文件怎么样?

What is the most efficient mechanism (in respect to data transferred and disk space used) to get the contents of a single file from a remote git repository?

So far I've managed to come up with:

git clone --no-checkout --depth 1 [email protected]:foo/bar.git && cd bar && git show HEAD:path/to/file.txt

This still seems overkill.

What about getting multiple files from the repo?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(25

你的笑 2024-08-03 03:12:18

在 git 版本 1.7.9.5 中,这似乎可以从远程导出单个文件,

git archive --remote=ssh://host/pathto/repo.git HEAD README.md | tar xO

这将捕获文件 README.md 的内容。

In git version 1.7.9.5 this seems to work to export a single file from a remote

git archive --remote=ssh://host/pathto/repo.git HEAD README.md | tar xO

This will cat the contents of the file README.md.

吾家有女初长成 2024-08-03 03:12:18

Jakub答案git archive 生成 tar 或 zip 存档,因此您需要通过 tar 通过管道传输输出以获取文件内容:

git archive --remote=git://git.foo.com/project.git HEAD:path/to/directory filename | tar -x

将在当前目录中从远程存储库的 HEAD 中保存“filename”的副本。

:path/to/directory 部分是可选的。 如果排除,获取的文件将保存到<当前工作目录>/path/to/directory/filename

此外,如果您想启用git archive --remote< /code> 在 git-daemon 托管的 Git 存储库上,您需要启用 daemon.uploadarch 配置选项。 请参阅 https://kernel.org/pub/software/scm/ git/docs/git-daemon.html

Following on from Jakub's answer. git archive produces a tar or zip archive, so you need to pipe the output through tar to get the file content:

git archive --remote=git://git.foo.com/project.git HEAD:path/to/directory filename | tar -x

Will save a copy of 'filename' from the HEAD of the remote repository in the current directory.

The :path/to/directory part is optional. If excluded, the fetched file will be saved to <current working dir>/path/to/directory/filename

In addition, if you want to enable use of git archive --remote on Git repositories hosted by git-daemon, you need to enable the daemon.uploadarch config option. See https://kernel.org/pub/software/scm/git/docs/git-daemon.html

弄潮 2024-08-03 03:12:18

如果部署了Web界面(如gitweb、cgit、Gitorious、ginatra),您可以使用它下载单个文件(“原始”或“普通”视图)。

如果对方启用了它,您可以使用git archive 的 '--remote=' 选项(并可能将其限制为给定文件所在的目录),例如:

$ git archive [email protected]:foo/bar.git --prefix=path/to/ HEAD:path/to/ |  tar xvf -

If there is web interface deployed (like gitweb, cgit, Gitorious, ginatra), you can use it to download single file ('raw' or 'plain' view).

If other side enabled it, you can use git archive's '--remote=<URL>' option (and possibly limit it to a directory given file resides in), for example:

$ git archive [email protected]:foo/bar.git --prefix=path/to/ HEAD:path/to/ |  tar xvf -
情绪操控生活 2024-08-03 03:12:18

一般情况下不会,但如果您使用 Github:

对我来说 wget 到原始 url 是下载特定文件的最佳和最简单的方法。

在浏览器中打开文件,然后单击“Raw”按钮。 现在刷新您的浏览器,复制 URL 并对其执行 wgetcurl 操作。

wget 示例:

wget 'https://github.abc.abc.com/raw/abc/folder1/master/folder2/myfile.py?token=DDDDnkl92Kw8829jhXXoxBaVJIYW-h7zks5Vy9I-wA%3D%3D' -O myfile.py

卷曲示例:

curl 'https://example.com/raw.txt' > savedFile.txt

Not in general but if you are using Github:

For me wget to the raw url turned out to be the best and easiest way to download one particular file.

Open the file in the browser and click on "Raw" button. Now refresh your browser, copy the url and do a wget or curl on it.

wget example:

wget 'https://github.abc.abc.com/raw/abc/folder1/master/folder2/myfile.py?token=DDDDnkl92Kw8829jhXXoxBaVJIYW-h7zks5Vy9I-wA%3D%3D' -O myfile.py

Curl example:

curl 'https://example.com/raw.txt' > savedFile.txt
金兰素衣 2024-08-03 03:12:18

从远程导出单个文件:

git archive --remote=ssh://host/pathto/repo.git HEAD README.md | tar -x

这会将文件 README.md 下载到您的当前目录。

如果您希望将文件内容导出到 STDOUT:

git archive --remote=ssh://host/pathto/repo.git HEAD README.md | tar -xO

您可以在命令末尾提供多个路径。

To export a single file from a remote:

git archive --remote=ssh://host/pathto/repo.git HEAD README.md | tar -x

This will download the file README.md to your current directory.

If you want the contents of the file exported to STDOUT:

git archive --remote=ssh://host/pathto/repo.git HEAD README.md | tar -xO

You can provide multiple paths at the end of the command.

黯淡〆 2024-08-03 03:12:18

如果没有其他答案有效(即限制性 GitLab 访问),您可以通过以下方式执行“选择性签出”:

  1. git clone --no-checkout --depth=1 --no-tags URL
  2. git Restore --staged DIR-OR-FILE
  3. git checkout DIR-OR-FILE

虽然此解决方案 100% git 兼容并且您可以签出目录,但它不是磁盘或网络最佳选择就像对文件执行 wget/curl 一样。

If no other answer worked (i.e. restrictive GitLab access), you can do a "selective-checkout" by:

  1. git clone --no-checkout --depth=1 --no-tags URL
  2. git restore --staged DIR-OR-FILE
  3. git checkout DIR-OR-FILE

Although this solution is 100% git compliant and you can checkout a directory, it's not disk nor network optimal as doing a wget/curl on a file.

终遇你 2024-08-03 03:12:18

我用这种方式解决了:

git archive --remote=ssh://[email protected]/user/mi-repo.git BranchName /path-to-file/file_name | tar -xO /path-to-file/file_name > /path-to-save-the-file/file_name

如果你愿意,你可以将“BranchName”替换为“HEAD”

I solved in this way:

git archive --remote=ssh://[email protected]/user/mi-repo.git BranchName /path-to-file/file_name | tar -xO /path-to-file/file_name > /path-to-save-the-file/file_name

If you want, you could replace "BranchName" for "HEAD"

錯遇了你 2024-08-03 03:12:18

这对我来说似乎是一个解决方案: http://gitready.com/intermediate/2009/02/27/get-a-file-from-a-specific-revision.html

git show HEAD~4:index.html > local_file

其中 4 表示四个版本now 和 ~ 是评论中提到的波浪号。

It looks like a solution to me: http://gitready.com/intermediate/2009/02/27/get-a-file-from-a-specific-revision.html

git show HEAD~4:index.html > local_file

where 4 means four revision from now and ~ is a tilde as mentioned in the comment.

英雄似剑 2024-08-03 03:12:18

这里的一些答案的微妙变体回答了OP的问题:

git archive [email protected]:foo/bar.git \
  HEAD path/to/file.txt | tar -xO path/to/file.txt > file.txt

A nuanced variant of some of the answers here that answers the OP's question:

git archive [email protected]:foo/bar.git \
  HEAD path/to/file.txt | tar -xO path/to/file.txt > file.txt
东京女 2024-08-03 03:12:18

我用这个

$ cat ~/.wgetrc
check_certificate = off

$ wget https://raw.github.com/jquery/jquery/master/grunt.js
HTTP request sent, awaiting response... 200 OK
Length: 11339 (11K) [text/plain]
Saving to: `grunt.js'

I use this

$ cat ~/.wgetrc
check_certificate = off

$ wget https://raw.github.com/jquery/jquery/master/grunt.js
HTTP request sent, awaiting response... 200 OK
Length: 11339 (11K) [text/plain]
Saving to: `grunt.js'
清风无影 2024-08-03 03:12:18

这是特定于 GitHub 上托管的 git 存储库的。

请尝试使用 Github 命令行应用程序 gh'api' 命令来进行经过身份验证的调用Github 的“获取存储库内容”端点。

基本命令是:

$gh api /repos/{owner}/{repo}/contents/<path_to_the_file>

作为一个额外的好处,当您从包含您尝试从中获取文件的存储库的克隆的目录中执行此操作时,{owner} 和 {repo} 部分将自动填充。

https://docs.github.com/en/rest/reference /repos#get-repository-content

响应将是一个 JSON 对象。 如果是 确实指向一个文件,JSON 将包含“大小”、“名称”、多个用于访问该文件的 url 字段,以及一个“内容”字段,它是文件内容的 Base64 编码版本。

要获取文件内容,您可以卷曲“download_url”的值,或者仅解码“content”字段。 您可以通过管道传递 base64 命令来做到这一点,如下所示:

$gh api /repos/{owner}/{repo}/contents/<path-to-the-file> --jq '.content' | base64 -d

This is specific for git repos hosted on GitHub

Try the 'api' command of Github's command line app, gh, to make an authenticated call to Github's 'get repository contents' endpoint.

The basic command is:

$gh api /repos/{owner}/{repo}/contents/<path_to_the_file>

As an added bonus, when you do this from inside a directory that contains a clone of the repo you're trying to get the file from, the {owner} and {repo} part will be automatically filled in.

https://docs.github.com/en/rest/reference/repos#get-repository-content

The response will be a JSON object. If the <path_to_the_file> indeed points to a file, the JSON will include a 'size', 'name', several url fields to access the file, as well as a 'content' field, which is a base64 encoded version of the file contents.

To get the file contents, you can curl the value of the "download_url", or just decode the 'content' field. You can do that by piping the base64 command, like this:

$gh api /repos/{owner}/{repo}/contents/<path-to-the-file> --jq '.content' | base64 -d
撑一把青伞 2024-08-03 03:12:18

在我看来,使用以下内容是最简单的方法:

wget https://github.com/name/folder/file.zip?raw=true

It seems to me the easiest way to use the following:

wget https://github.com/name/folder/file.zip?raw=true
末が日狂欢 2024-08-03 03:12:18

如果您的存储库支持令牌(例如 GitLab),则为您的用户生成一个令牌,然后导航到您要下载的文件并单击 RAW 输出以获取 URL。 要下载文件,请使用:

curl --silent --request GET --header 'PRIVATE-TOKEN: replace_with_your_token' \
'http://git.example.com/foo/bar.sql' --output /tmp/bar.sql

If you repository supports tokens (for example GitLab) then generate a token for your user then navigate to the file you will download and click on RAW output to get the URL. To download the file use:

curl --silent --request GET --header 'PRIVATE-TOKEN: replace_with_your_token' \
'http://git.example.com/foo/bar.sql' --output /tmp/bar.sql
掌心的温暖 2024-08-03 03:12:18

对于单个文件,只需使用 wget 命令。

首先,按照下图点击“raw”获取url,否则您将下载嵌入html的代码。
输入图片description here

然后,浏览器将打开一个新页面,其 url 以 https://raw 开头。 githubusercontent.com/...

只需在终端中输入命令:

#wget https://raw.githubusercontent.com/...

一会儿文件就会放入您的文件夹中。

For single file, just use wget command.

First, follow the pic below to click "raw" to get the url, otherwise you will download code embedded in html.
enter image description here

Then, the browser will open a new page with url start with https://raw.githubusercontent.com/...

just enter the command in the terminal:

#wget https://raw.githubusercontent.com/...

A while the file will put in your folder.

满栀 2024-08-03 03:12:18

如果您的 Git 存储库托管在 Azure-DevOps (VSTS) 上,您可以使用 Rest API

该 API 的格式如下所示:

 https://dev.azure.com/{organization}/_apis/git/repositories/{repositoryId}/items?path={pathToFile}&api-version=4.1?download=true

例如:

 https://dev.azure.com/{organization}/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/items?scopePath=/MyWebSite/MyWebSite/Views/Home/_Home.cshtml&download=true&api-version=4.1

If your Git repository hosted on Azure-DevOps (VSTS) you can retrieve a single file with Rest API.

The format of this API looks like this:

 https://dev.azure.com/{organization}/_apis/git/repositories/{repositoryId}/items?path={pathToFile}&api-version=4.1?download=true

For example:

 https://dev.azure.com/{organization}/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/items?scopePath=/MyWebSite/MyWebSite/Views/Home/_Home.cshtml&download=true&api-version=4.1
娇女薄笑 2024-08-03 03:12:18

以下 2 个命令对我有用:

git archive --remote={remote_repo_git_url} {branch} {file_to_download} -o {tar_out_file}

下载 file_to_download 作为 tar< /code> 来自远程存储库的 branch 的存档,其 url 为 remote_repo_git_url 并将其存储在 tar_out_file

tar -x -f {tar_out_file} .tartar_out_file 中提取 file_to_download

The following 2 commands worked for me:

git archive --remote={remote_repo_git_url} {branch} {file_to_download} -o {tar_out_file}

Downloads file_to_download as tar archive from branch of remote repository whose url is remote_repo_git_url and stores it in tar_out_file

tar -x -f {tar_out_file}.tar extracts the file_to_download from tar_out_file

我为君王 2024-08-03 03:12:18

我使用curl,它适用于公共存储库或通过Web 界面使用https 基本身份验证的存储库。

curl -L --retry 20 --retry-delay 2 -O https://github.com/ACCOUNT/REPO/raw/master/PATH/TO/FILE/FILE.TXT -u 用户:密码

我已经在 github 和 bitbucket 上测试过它,两者都适用。

I use curl, it works with public repos or those using https basic authentication via a web interface.

curl -L --retry 20 --retry-delay 2 -O https://github.com/ACCOUNT/REPO/raw/master/PATH/TO/FILE/FILE.TXT -u USER:PASSWORD

I've tested it on github and bitbucket, works on both.

满身野味 2024-08-03 03:12:18

Yisrael Dov 的答案很简单,但它不允许压缩。 您可以使用 --format=zip,但您无法像使用 tar 一样使用管道命令直接解压缩它,因此您需要将其保存为临时文件。 这是一个脚本:

#!/bin/bash

BASENAME=$0

function usage {
    echo "usage: $BASENAME <remote-repo> <file> ..."
    exit 1
}

[ 2 -gt "$#" ] && { usage; }

REPO=$1
shift
FILES=$@

TMPFILE=`mktemp`.zip
git archive -9 --remote=$REPO HEAD $FILES -o $TMPFILE
unzip $TMPFILE
rm $TMPFILE

这也适用于目录。

Yisrael Dov's answer is the straightforward one, but it doesn't allow compression. You can use --format=zip, but you can't directly unzip that with a pipe command like you can with tar, so you need to save it as a temporary file. Here's a script:

#!/bin/bash

BASENAME=$0

function usage {
    echo "usage: $BASENAME <remote-repo> <file> ..."
    exit 1
}

[ 2 -gt "$#" ] && { usage; }

REPO=$1
shift
FILES=$@

TMPFILE=`mktemp`.zip
git archive -9 --remote=$REPO HEAD $FILES -o $TMPFILE
unzip $TMPFILE
rm $TMPFILE

This works with directories too.

明明#如月 2024-08-03 03:12:18

Github 企业解决方案

HTTPS_DOMAIN=https://git.your-company.com
ORGANISATION=org
REPO_NAME=my-amazing-library
FILE_PATH=path/to/some/file
BRANCH=develop
GITHUB_PERSONAL_ACCESS_TOKEN=<your-access-token>

URL="${HTTPS_DOMAIN}/raw/${ORGANISATION}/${REPO_NAME}/${BRANCH}/${FILE_PATH}"

curl -H "Authorization: token ${GITHUB_PERSONAL_ACCESS_TOKEN}" ${URL} > "${FILE_PATH}"

Github Enterprise Solution

HTTPS_DOMAIN=https://git.your-company.com
ORGANISATION=org
REPO_NAME=my-amazing-library
FILE_PATH=path/to/some/file
BRANCH=develop
GITHUB_PERSONAL_ACCESS_TOKEN=<your-access-token>

URL="${HTTPS_DOMAIN}/raw/${ORGANISATION}/${REPO_NAME}/${BRANCH}/${FILE_PATH}"

curl -H "Authorization: token ${GITHUB_PERSONAL_ACCESS_TOKEN}" ${URL} > "${FILE_PATH}"
两个我 2024-08-03 03:12:18

如果你想从特定的哈希+远程存储库获取文件,我尝试过 git-archive 但它不起作用。

您必须使用 git clone,一旦克隆存储库,您就必须使用 git-archive 才能使其工作。

我在 git 存档中发布了一个关于如何更简单地进行操作的问题来自远程的特定哈希

If you want to get a file from a specific hash + a remote repository I've tried git-archive and it didn't work.

You would have to use git clone and once the repository is cloned you would have then to use git-archive to make it work.

I post a question about how to do it more simpler in git archive from a specific hash from remote

旧竹 2024-08-03 03:12:18

对于直接从浏览器(我使用 safari...)的 bitbucket,右键单击“查看原始数据”并选择“下载链接文件”:

在此处输入图像描述

for bitbucket directly from browser (I used safari...) right-click on 'View Raw" and choose "Download Linked File":

enter image description here

碍人泪离人颜 2024-08-03 03:12:18

如果您不介意克隆整个目录,这个小 bash/zsh 函数将获得将单个文件克隆到当前目录的最终结果(通过将存储库克隆到临时目录并随后将其删除)。

优点:你只能得到你想要的文件

缺点:你仍然需要等待整个存储库克隆

git-single-file () {
        if [ $# -lt 2 ]
        then
                echo "Usage: $0 <repo url> <file path>"
                return
        fi
        TEMP_DIR=$(mktemp -d)
        git clone $1 $TEMP_DIR
        cp $TEMP_DIR/$2 .
        rm -rf $TEMP_DIR
}

If you don't mind cloning the entire directory, this small bash/zsh function will have the end result of cloning a single file into your current directory (by cloning the repo into a temp directory and removing it afterwards).

Pro: You only get the file you want

Con: You still have to wait for the whole repo to clone

git-single-file () {
        if [ $# -lt 2 ]
        then
                echo "Usage: $0 <repo url> <file path>"
                return
        fi
        TEMP_DIR=$(mktemp -d)
        git clone $1 $TEMP_DIR
        cp $TEMP_DIR/$2 .
        rm -rf $TEMP_DIR
}
z祗昰~ 2024-08-03 03:12:18

如果您的目标只是下载文件,那么有一个名为gget的无忧应用程序:

gget github.com/gohugoio/hugo 'hugo_extended_*_Linux-ARM.deb'

上面的示例将从hugo存储库下载单个文件。

https://github.com/dpb587/gget

If your goal is just to download the file there's a hassle-free application called gget:

gget github.com/gohugoio/hugo 'hugo_extended_*_Linux-ARM.deb'

The above example would download single file from hugo repository.

https://github.com/dpb587/gget

舞袖。长 2024-08-03 03:12:18

我创建了一个到目前为止对我有用的小 bash 脚本:

githubLink="$1"
outputFile="$2"

if [ -z "$githubLink" ]; then
    echo "please enter link to gather as first argument"
fi

if [ -z "$outputFile" ]; then
        outputFile="output.txt"
        echo "to change filename from output, add the desired filename as second argument"
fi

username=$(echo "$githubLink" | cut -d'/' -f4)
repo=$(echo "$githubLink" | cut -d'/' -f5)
branch=$(echo "$githubLink" | cut -d'/' -f7)
filePath=$(echo "$githubLink" | cut -d'/' -f8-)
fullPath="https://raw.githubusercontent.com/$username/$repo/$branch/$filePath"

echo "$fullPath"

$(curl "$fullPath">"$outputFile")

Simply ./(scriptName.sh) (原始 github 链接)(输出文件名)

我只是用它来获取 Pi Pico 的链接文件,它运行良好。
这里的关键是不要使用原始链接作为来源,而是解析
原始链接并创建原始文件的链接。 我不确定这样做是否有任何负面影响,所以如果有人比我对原始 github 内容有更多的见解,请随时添加。 作为旁注,请确保使用完整链接(即 https://),否则剪辑将会被关闭。

I created a little bash script that has worked for me so far:

githubLink="$1"
outputFile="$2"

if [ -z "$githubLink" ]; then
    echo "please enter link to gather as first argument"
fi

if [ -z "$outputFile" ]; then
        outputFile="output.txt"
        echo "to change filename from output, add the desired filename as second argument"
fi

username=$(echo "$githubLink" | cut -d'/' -f4)
repo=$(echo "$githubLink" | cut -d'/' -f5)
branch=$(echo "$githubLink" | cut -d'/' -f7)
filePath=$(echo "$githubLink" | cut -d'/' -f8-)
fullPath="https://raw.githubusercontent.com/$username/$repo/$branch/$filePath"

echo "$fullPath"

$(curl "$fullPath">"$outputFile")

Simply ./(scriptName.sh) (original github link) (output file name)

I just used it to grab the linkfile for a Pi Pico and it worked well.
The key here is to not use the original link as the source, rather parse the
original link and create a link to the raw file. I'm unsure if there's any negative side effects to doing it this way, so if anyone has any more insight than I do on raw github content feel free to add. As a side note, make sure to use the full link (i.e https://) otherwise the cuts will be off.

孤者何惧 2024-08-03 03:12:18

与@Steven Penny的答案相关,我也使用wget。 此外,为了决定将输出发送到哪个文件,我使用 -O 。

如果您使用 gitlabs,该 url 的另一种可能性是:

wget "https://git.labs.your-server/your-repo/raw/master/<path-to-file>" -O <output-file>

除非您有证书或从受信任的服务器访问 gitlabs 安装,否则您需要 --no-check-certificate 正如 @Kos 所说。 我更喜欢这样做而不是修改 .wgetrc ,但这取决于您的需要。

如果它是一个大文件,您可以考虑使用 wget 的 -c 选项。 如果之前的意图在中间失败,则能够从您离开的位置继续下载文件。

Related to @Steven Penny's answer, I also use wget. Furthermore, to decide which file to send the output to I use -O .

If you are using gitlabs another possibility for the url is:

wget "https://git.labs.your-server/your-repo/raw/master/<path-to-file>" -O <output-file>

Unless you have the certificate or you access from a trusted server for the gitlabs installation you need --no-check-certificate as @Kos said. I prefer that rather than modifying .wgetrc but it depends on your needs.

If it is a big file you might consider using -c option with wget. To be able to continue downloading the file from where you left it if the previous intent failed in the middle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文