如何在GitHub上下载指定的文件?

一、下载全部文件

如下图所示:

1、可以点击 Download Zip下载压缩包

2、使用命令行,即:使用git clone,如下:

git clone 连接地址(即是中间框框里面的地址)

 

二、下载指定文件

1、先点击进入需要下载的子文件夹或是文件

2、在浏览器把地址复制下来,然后做下修改,比如:

原地址:https://github.com/mrdoob/three.js/tree/dev/src

把tree/dev部分改成trunk

修改后的地址:https://github.com/mrdoob/three.js/trunk/src

统一来说就是把tree/***改成trunk

然后使用 svn checkout 修改后的地址

<think>我们面对的问题:从私有GitHub仓库下载单个文件 用户需求:提供几种方法,例如使用命令行工具或脚本 根据引用[1]:克隆私有仓库可以使用`git clone`命令,但这是克隆整个仓库。我们需要的是单个文件。 引用[3]提到使用TortoiseSVN通过SVN的方式下载单个文件夹,但同样可以用于单个文件吗?而且用户要求的是命令行方法。 引用[4]提到一个浏览器插件,但用户可能更倾向于命令行。 因此,我们考虑以下方法: 方法1:使用GitHub的API(需要认证) - 步骤1:生成个人访问令牌(Personal Access Token, PAT) - 步骤2:使用curl或wget通过API下载文件 方法2:使用svn(适用于文件夹或文件) - 步骤1:安装svn客户端(如TortoiseSVN或命令行svn) - 步骤2:使用svn导出文件 方法3:使用git sparse-checkout(需要克隆整个仓库,但只检出指定文件,适合大仓库中少量文件) - 步骤1:初始化一个空仓库 - 步骤2:启用sparse-checkout - 步骤3:指定下载文件 - 步骤4:拉取文件 方法4:使用第三方工具(如gh cli,GitHub官方命令行工具) 由于是私有仓库,都需要认证。 下面分别详细说明: ### 方法1:使用GitHub API(推荐用于单个文件) 1. 在GitHub上生成一个个人访问令牌(PAT),需要至少包含`repo`权限(对于私有仓库)。 2. 使用curl命令下载文件。注意,GitHub API的URL格式为: `https://api.github.com/repos/:owner/:repo/contents/:path` 但是,这个API返回的是文件的元数据,其中包含一个`download_url`字段,我们可以用这个URL来下载文件内容。 然而,更直接的方式是使用原始文件URL(raw.githubusercontent.com),但需要认证。 原始文件URL格式: `https://raw.githubusercontent.com/:owner/:repo/:branch/:path` 对于私有仓库,我们可以在curl命令中加入认证头(使用PAT)。 示例命令: ```bash curl -L -H "Authorization: token <your-token>" \ -o <output-filename> \ "https://raw.githubusercontent.com/<owner>/<repo>/<branch>/<path-to-file>" ``` 例如,下载用户`apirrone`的私有仓库`Open_Duck_Mini`的`main`分支下的`BEST_WALK_ONNX_2.onnx`文件: ```bash curl -L -H "Authorization: token ghp_abcdef1234567890" \ -o BEST_WALK_ONNX_2.onnx \ "https://raw.githubusercontent.com/apirrone/Open_Duck_Mini/main/BEST_WALK_ONNX_2.onnx" ``` 注意:将`<your-token>`替换为你的实际令牌,并确保令牌有权限访问该私有仓库。 ### 方法2:使用svn(适合文件夹或单个文件) 引用[3]中提到了使用TortoiseSVN,但这里我们使用命令行svn。 1. 安装svn客户端(例如在Ubuntu上:`sudo apt install subversion`) 2. 使用以下命令导出单个文件(注意:svn的URL格式与git不同): - 私有仓库需要认证,svn会提示输入用户名和密码(PAT可以作为密码) 对于GitHub,svn访问的URL格式为: `https://github.com/<owner>/<repo>/trunk/<path>` 或 `https://github.com/<owner>/<repo>/branches/<branch>/<path>` 但是,请注意,GitHub的svn支持只适用于trunk(对应默认分支)和branches。对于单个文件,我们可以使用: ```bash svn export --username <username> --password <token> \ https://github.com/<owner>/<repo>/trunk/<path-to-file> \ [本地文件名] ``` 例如: ```bash svn export --username your_github_username --password ghp_abcdef1234567890 \ https://github.com/apirrone/Open_Duck_Mini/trunk/BEST_WALK_ONNX_2.onnx \ BEST_WALK_ONNX_2.onnx ``` 注意:这里`trunk`对应的是默认分支(通常是main或master),如果文件在其他分支,请使用`branches/<branch-name>/`。 但是,svn export会下载文件并删除.svn元数据(因为是导出),所以得到的就是纯文件。 ### 方法3:使用git sparse-checkout(适合大仓库中下载少量文件) 步骤: ```bash # 初始化一个空仓库 mkdir repo && cd repo git init # 设置允许稀疏检出 git config core.sparseCheckout true # 添加远程仓库(使用私有仓库的URL,注意需要认证) git remote add origin https://github.com/<owner>/<repo>.git # 指定要检出的文件路径(相对仓库根目录) echo "<path-to-file>" >> .git/info/sparse-checkout # 拉取文件(需要认证,可以使用用户名和PAT) git pull origin <branch> --depth=1 ``` 例如: ```bash mkdir temp_repo && cd temp_repo git init git config core.sparseCheckout true git remote add origin https://github.com/apirrone/Open_Duck_Mini.git echo "BEST_WALK_ONNX_2.onnx" >> .git/info/sparse-checkout git pull origin main --depth=1 ``` 然后,文件会出现在当前目录下。注意,这里我们使用了`--depth=1`来减少历史记录,加快下载。 认证:在pull时,git会要求输入用户名和密码,密码使用PAT。 ### 方法4:使用GitHub CLI (gh) 1. 安装gh:https://cli.github.com/ 2. 登录:`gh auth login`(按照提示进行,需要提供PAT) 3. 使用以下命令下载文件: ```bash gh repo view <owner>/<repo> --json name # 验证登录 # 下载文件 gh api -H "Accept: application/vnd.github.raw" \ /repos/<owner>/<repo>/contents/<path-to-file>?ref=<branch> \ > <output-file> ``` 例如: ```bash gh api -H "Accept: application/vnd.github.raw" \ /repos/apirrone/Open_Duck_Mini/contents/BEST_WALK_ONNX_2.onnx?ref=main \ > BEST_WALK_ONNX_2.onnx ``` 注意:gh命令已经使用登录时的认证,所以无需额外输入令牌。 ### 总结 推荐方法: - 如果只需要下载单个文件,使用curl和API(方法1)最直接。 - 如果已经配置了GitHub CLI(gh),使用方法4更简单。 - 如果需要下载文件夹,svn(方法2)或sparse-checkout(方法3)更合适。 注意事项: - 令牌需要妥善保管,不要在公共场合泄露。 - 对于私有仓库,确保令牌有足够的权限(至少要有repo的读权限)。 根据用户需求,我们提供了四种方法,用户可以根据自己的环境和偏好选择。 §§相关问题§§ 1. 如何生成GitHub个人访问令牌(PAT)? 2. 如何配置GitHub CLI(gh)进行认证? 3. 使用svn下载GitHub文件时遇到认证失败怎么办? 4. 如何批量下载私有仓库中的多个文件?</think>要从私有 GitHub 仓库下载单个文件,以下是几种有效方法: ### 方法1:使用 cURL 命令行工具(推荐) ```bash # 生成个人访问令牌(PAT) https://github.com/settings/tokens # 需要勾选 repo 权限 # 下载命令 curl -L -H "Authorization: token ghp_your_token_here" \ -o scripts/BEST_WALK_ONNX_2.onnx \ "https://raw.githubusercontent.com/apirrone/Open_Duck_Mini/main/BEST_WALK_ONNX_2.onnx" ``` **参数说明**: - `-L`:跟随重定向 - `-H`:添加认证头(替换 `ghp_your_token_here` 为你的实际 PAT) - `-o`:指定输出路径 - URL 格式:`https://raw.githubusercontent.com/<用户>/<仓库>/<分支>/<文件路径>` ### 方法2:使用 GitHub CLI (gh) ```bash # 安装 GitHub CLI: https://cli.github.com gh auth login # 按提示完成认证 # 下载文件 gh api \ -H "Accept: application/vnd.github.raw" \ /repos/apirrone/Open_Duck_Mini/contents/BEST_WALK_ONNX_2.onnx?ref=main \ > scripts/BEST_WALK_ONNX_2.onnx ``` ### 方法3:使用 SVN(适合文件下载) ```bash # 安装 SVN 客户端 sudo apt install subversion # Ubuntu/Debian brew install subversion # macOS # 下载单个文件 svn export --username your_github_username \ --password ghp_your_token_here \ https://github.com/apirrone/Open_Duck_Mini/trunk/BEST_WALK_ONNX_2.onnx \ scripts/BEST_WALK_ONNX_2.onnx ``` ### 方法4:使用 Git 稀疏检出(适合大仓库) ```bash mkdir temp_repo && cd temp_repo git init git remote add origin https://github.com/apirrone/Open_Duck_Mini.git git config core.sparseCheckout true echo "BEST_WALK_ONNX_2.onnx" >> .git/info/sparse-checkout git pull origin main --depth=1 cp BEST_WALK_ONNX_2.onnx ../scripts/ cd .. && rm -rf temp_repo ``` ### 关键注意事项: 1. **认证要求**: - 所有方法都需要[创建个人访问令牌(PAT)](https://docs.github.com/zh/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens),勾选 `repo` 权限 - 令牌需保密,不在公共场合暴露 2. **URL 格式**: - 原始文件地址:`https://raw.githubusercontent.com/...`(非仓库浏览地址) - 分支名称:`main`/`master` 或具体分支名(示例中使用 `main`) 3. **路径验证**: ```bash # 检查文件是否下载成功 ls -lh scripts/BEST_WALK_ONNX_2.onnx file scripts/BEST_WALK_ONNX_2.onnx ``` > **安全提示**:对于自动化脚本,建议使用[GitHub Actions 密钥存储](https://docs.github.com/zh/actions/security-guides/encrypted-secrets)保护令牌,避免硬编码[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值