joh*_*ohn 2 windows powershell github
我使用这个 power shell 脚本从 Git Hub 存储库下载文件
$url = "https://gist.github.com/ . . ./test.jpg"
$output = "C:\Users\admin\Desktop\test.jpg"
$start_time = Get-Date
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $output)
#OR
(New-Object System.Net.WebClient).DownloadFile($url, $output)
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
Run Code Online (Sandbox Code Playgroud)
它适用于一个文件。如何使用 PowerShell 下载完整的存储库?我无法使用git pull.
编辑 这是我在真实存储库上尝试的内容。
存储库:https : //github.com/githubtraining/hellogitworld/archive/master.zip
这是一个测试代码,但它没有完全下载存储库
$url = "https://github.com/githubtraining/hellogitworld/archive/master.zip"
$output = "C:\Users\mycompi\Desktop\test\master.zip"
$start_time = Get-Date
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $output)
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
Run Code Online (Sandbox Code Playgroud)
您可以从以下 URL 下载位于当前 HEAD 的分支的压缩副本:
https://github.com/[owner]/[repository]/archive/[branch].zip
Run Code Online (Sandbox Code Playgroud)
例如,要在 GitHub 上下载 PowerShell 存储库的当前主分支,您需要执行以下操作:
$url = "https://github.com/PowerShell/PowerShell/archive/master.zip"
$output = "C:\Users\admin\Desktop\master.zip"
$start_time = Get-Date
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $output)
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
Run Code Online (Sandbox Code Playgroud)
你可以很容易地把它变成一个可重用的函数:
function Save-GitHubRepository
{
param(
[Parameter(Mandatory)]
[string]$Owner,
[Parameter(Mandatory)]
[string]$Project,
[Parameter()]
[string]$Branch = 'master'
)
$url = "https://github.com/$Owner/$Project/archive/$Branch.zip"
$output = Join-Path $HOME "Desktop\${Project}-${Branch}_($(Get-Date -Format yyyyMMddHHmm)).zip"
$start_time = Get-Date
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $output)
Write-Host "Time taken: $((Get-Date).Subtract($start_time).TotalSeconds) second(s)"
}
Run Code Online (Sandbox Code Playgroud)
然后像这样使用它:
PS C:\> Save-GitHubRepository PowerShell PowerShell
Run Code Online (Sandbox Code Playgroud)