如何使用PowerShell下载完整存储库?

时间:2018-01-31 17:03:06

标签: 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)" 

适用于一个文件。如何使用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)"

1 个答案:

答案 0 :(得分:3)

您可以从以下网址下载其当前HEAD的分支的压缩副本:

https://github.com/[owner]/[repository]/archive/[branch].zip

例如,要在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)" 

您可以轻松将其转换为可重复使用的功能:

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)" 
}

然后使用它:

PS C:\> Save-GitHubRepository PowerShell PowerShell