注意:此方法對(duì)Git-LFS方式上傳的文件可能無效!??!
一.打開PowerShel運(yùn)行命令
在 Windows 中,可以通過以下步驟打開 PowerShell 終端:
-
使用搜索功能:
- 點(diǎn)擊 Windows 按鈕(開始菜單左下角的 Windows 圖標(biāo))。
- 在搜索框中鍵入 "PowerShell" 或 "Windows PowerShell"。
- 在搜索結(jié)果中,選擇 "Windows PowerShell" 或 "Windows PowerShell ISE"。
-
使用菜單:
- 右鍵點(diǎn)擊 Windows 按鈕(開始菜單左下角的 Windows 圖標(biāo))。
- 選擇 "Windows PowerShell" 或 "Windows PowerShell ISE"。
-
使用運(yùn)行命令:
- 按下
Win + R
組合鍵,打開運(yùn)行對(duì)話框。 - 鍵入
powershell
或powershell_ise
,然后按 Enter 鍵。
- 按下
二.下載倉庫文件
2.1下載公開倉庫文件
$owner = " Your owner"
$repo = "Your? repo"
$branch = "Your branch"
$path = "Your path"
# API 請(qǐng)求 URL
$url = "https://api.github.com/repos/$owner/$repo/contents/$path?ref=$branch"
# 發(fā)送 API 請(qǐng)求
$response = Invoke-RestMethod -Uri $url
# 下載文件
foreach ($file in $response) {
? ? $fileUrl = $file.download_url
? ? $fileName = $file.name
? ? Invoke-WebRequest -Uri $fileUrl -OutFile $fileName
}
Write-Host "Files downloaded successfully."
假設(shè)下載HITCSLab2倉庫master分支下的文件夾"概率論論文",并將文件放到D:\111中,示例如下:
即可下載成功。
如果你想要使用 Python 腳本下載 HITCSLab2
倉庫下的文件夾 概率論論文
,你可以使用 requests
庫。確保你已經(jīng)安裝這個(gè)庫:
pip install requests
然后,你可以使用以下 Python 腳本:
import requests
import os
# 倉庫信息
owner = "HITCSzwx"
repo = "HITCSLab2"
branch = "master"
path = "概率論論文"
# GitHub API 請(qǐng)求 URL
url = f"https://api.github.com/repos/{owner}/{repo}/contents/{path}?ref={branch}"
# 發(fā)送 API 請(qǐng)求
response = requests.get(url)
response.raise_for_status() # 檢查請(qǐng)求是否成功
# 下載文件
for file in response.json():
file_url = file["download_url"]
file_name = os.path.join(path, file["name"])
file_response = requests.get(file_url)
file_response.raise_for_status()
with open(file_name, "wb") as f:
f.write(file_response.content)
print(f"Files from '{path}' downloaded successfully.")
這個(gè)腳本使用 Python 中的 requests
庫,通過 GitHub API 獲取指定倉庫、分支和文件夾路徑下的文件列表,并下載這些文件到當(dāng)前目錄。確保替換腳本中的 owner
、repo
、branch
和 path
等參數(shù)為你的實(shí)際值。
請(qǐng)記得在運(yùn)行腳本之前安裝 requests
庫。
?2.2下載私有倉庫文件
$owner = "HITCSzwx"
$repo = "HITCSLab2"
$branch = "master"
$path = "概率論論文"
# GitHub Personal Access Token
$token = "<YOUR_ACCESS_TOKEN>"
# API 請(qǐng)求 URL
$url = "https://api.github.com/repos/$owner/$repo/contents/$path?ref=$branch"
# 設(shè)置請(qǐng)求頭
$headers = @{
Authorization = "Bearer $token"
Accept = "application/vnd.github.v3.raw"
}
# 發(fā)送 API 請(qǐng)求
$response = Invoke-RestMethod -Uri $url -Headers $headers
# 下載文件
foreach ($file in $response) {
$fileUrl = $file.download_url
$fileName = $file.name
Invoke-WebRequest -Uri $fileUrl -OutFile $fileName
}
Write-Host "Files downloaded successfully."
對(duì)于token怎么獲得
-
在 GitHub 上,登錄到你的賬戶。
-
點(diǎn)擊右上角的頭像,然后選擇 "Settings"(設(shè)置)。
-
在左側(cè)導(dǎo)航欄中,選擇 "Developer settings"(開發(fā)者設(shè)置)。
-
在 "Personal access tokens"(訪問令牌)部分,點(diǎn)擊 "Generate new token"(生成令牌)。
-
在 "Note" 字段中,為你的令牌提供一個(gè)描述性的名稱,以便日后識(shí)別這個(gè)令牌是用于何種目的。
-
在 "Select scopes"(選擇范圍)部分,選擇需要的權(quán)限。如果只是需要訪問公共倉庫,只需選擇 "public_repo" 權(quán)限;如果需要訪問私有倉庫,還需選擇 "repo" 權(quán)限。如果要訪問私有倉庫的 LFS 文件,還需要選擇 "read:user" 和 "read:org"。
-
點(diǎn)擊 "Generate token"(生成令牌)。
-
復(fù)制生成的令牌。請(qǐng)注意,這是你唯一的機(jī)會(huì)復(fù)制令牌,因?yàn)?GitHub 不會(huì)存儲(chǔ)令牌的內(nèi)容。文章來源:http://www.zghlxwxcb.cn/news/detail-790608.html
注意:此方法對(duì)Git-LFS方式上傳的文件可能無效?。?!??文章來源地址http://www.zghlxwxcb.cn/news/detail-790608.html
到了這里,關(guān)于無需克隆整個(gè)倉庫,下載Github特定文件夾內(nèi)的文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!