如何使用Powershell下载和安装Windows Git客户端

cld4siwp  于 2023-01-19  发布在  Git
关注(0)|答案(4)|浏览(394)

我需要编写自动化的powershell脚本来从gihtub克隆仓库,但是我需要使用命令行来安装git,你能告诉我如何在windows上使用命令行来下载和安装git,而不需要做任何手工操作吗?
先谢了!

muk1a3rh

muk1a3rh1#

我也想不使用chocolatey也能做到这一点。下面的代码对我来说很有效,使用powershell下载并安装64位版本的git-for-windows:

# get latest download url for git-for-windows 64-bit exe
$git_url = "https://api.github.com/repos/git-for-windows/git/releases/latest"
$asset = Invoke-RestMethod -Method Get -Uri $git_url | % assets | where name -like "*64-bit.exe"
# download installer
$installer = "$env:temp\$($asset.name)"
Invoke-WebRequest -Uri $asset.browser_download_url -OutFile $installer
# run installer
$git_install_inf = "<install inf file>"
$install_args = "/SP- /VERYSILENT /SUPPRESSMSGBOXES /NOCANCEL /NORESTART /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS /LOADINF=""$git_install_inf"""
Start-Process -FilePath $installer -ArgumentList $install_args -Wait

其中<install inf file>是一个包含git安装参数的文件的路径,作为一个例子,这是我正在使用的文件(我通过使用/SAVEINF=<install inf file>参数运行git installer exe得到的):

[Setup]
Lang=default
Dir=C:\Program Files\Git
Group=Git
NoIcons=0
SetupType=default
Components=ext,ext\shellhere,ext\guihere,gitlfs,assoc,autoupdate
Tasks=
EditorOption=VIM
CustomEditorPath=
PathOption=Cmd
SSHOption=OpenSSH
TortoiseOption=false
CURLOption=WinSSL
CRLFOption=LFOnly
BashTerminalOption=ConHost
PerformanceTweaksFSCache=Enabled
UseCredentialManager=Enabled
EnableSymlinks=Disabled
EnableBuiltinInteractiveAdd=Disabled

通过以下对类似问题的回答了解了安装参数文件:https://superuser.com/a/1005634/1104046.
运行这个命令需要重启shell才能更新Path环境变量并让git命令正常工作,或者,你可以用以下命令更新当前的powershell Path环境变量

$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")

然后在Path变量中包含git.exe的路径。

sqyvllje

sqyvllje2#

您可以使用chocolatey编写Git安装脚本。
软件包记录在https://chocolatey.org/packages/git
1.首先,您必须install chocolatey, but that can be done on the command line
1.则命令为:
choco安装-y git

xyhw6mcr

xyhw6mcr3#

只需一个命令解决方案
打开Powershell并键入命令:winget install --id Git.Git -e --source winget
重新启动powershell,命令行中将显示Git。

c9x0cxw0

c9x0cxw04#

通过配置项中的Dockerfile进行安装

FROM mcr.microsoft.com/windows/servercore:ltsc2019

# Assuming that you have "install-git-instructions.txt" file in the directory where this Dockerfile is located

ADD install-git-instructions.txt .

RUN powershell.exe -Command \
  $ErrorActionPreference = 'Stop'; \

  $ErrorActionPreference = 'Stop'; \
  (New-Object System.Net.WebClient).DownloadFile('https://github.com/git-for-windows/git/releases/download/v2.39.0.windows.1/Git-2.39.0-64-bit.exe','c:\Git-2.39.0-64-bit.exe') ; \
  Start-Process c:\Git-2.39.0-64-bit.exe -ArgumentList '/LOADINF=""C:\install-git-instructions.txt"" /SILENT' -Wait ;\
  Remove-Item c:\Git-2.39.0-64-bit.exe -Force;\

# Example Contents for install-git-instructions.txt 
#[Setup]
#Lang=default
#Dir=C:\Program Files\Git
#Group=Git
#NoIcons=0
#SetupType=default
#Components=gitlfs
#Tasks=
#PathOption=Cmd
#SSHOption=OpenSSH
#CURLOption=OpenSSL
#CRLFOption=CRLFAlways
#BashTerminalOption=MinTTY
#PerformanceTweaksFSCache=Enabled
#UseCredentialManager=Disabled
#EnableSymlinks=Disabled

相关问题