https://gitforwindows.org/可以选择将bash放入PowerShell中。我需要这样做,所以没有安装WSL等。我需要安装git无人值守,也就是说,只有命令行。像这这样的现有教程只使用PowerShell启动安装程序,但是我必须使用鼠标来安装东西。
那么,如何使用PowerShell使用PowerShell来安装git呢?
更新:
我试过了
Write-Host "Installing Git for windows..." -ForegroundColor Cyan
$exePath = "$env:TEMP\git.msi"
Write-Host "Downloading..."
(New-Object Net.WebClient).DownloadFile('https://github.com/git-for-windows/git/releases/download/v2.37.1.windows.1/Git-2.37.1-64-bit.exe', $exePath)
Write-Host "Installing..."
Start-Process msiexec.exe -Wait -ArgumentList '$exePath /NORESTART /NOCANCEL /SP- /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS /COMPONENTS="icons,ext\reg\shellhere,assoc,assoc_sh" /LOG="C:git-for-windows.log"'
git --version
bash但它被困在“安装.”也不打印任何其他输出。
发布于 2022-08-11 06:59:24
有两个问题:
msiexec.exe。安装程序本身已经具有执行静默安装的参数。只需按如下方式执行:
$exePath = "$env:TEMP\git.exe“启动-Process $exePath -Wait -ArgumentList '/NORESTART /NOCANCEL /SP- /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS /COMPONENTS=”图标,ext\reg\SP,assoc,assoc_sh“/LOG=”C:\git-for-windows.log“
但是:这将启动一个GUI。因此,您必须添加更多的参数,以使安装真正安静。进一步读:- [Git: Silent or Unattended Installation](https://github.com/git-for-windows/git/wiki/Silent-or-Unattended-Installation)
- [Git For Windows Silent Install Silent Arguments](https://superuser.com/q/944576/1077440)TL;DR:还添加了/VERYSILENT,您可能希望使用/LOADINF来自定义一些设置。
Process作用域中的环境变量不会自动更新。通过以下方式手动更新:
foreach(在“机器”、“用户”中的$level){$level%{#表示路径变量,如果它们还没有在其中,如果($_.Name -match ' Path $') { $_.Value =((Get-Content "Env:$($_.Name)") +“;$($_.Value) -split ';‘Select’-join ';{ "Env:$($_.Name)“}}
这段代码取自这个答案。在此之后,git --version和Get-Command git将工作。
完整脚本:
$exePath = "$env:TEMP\git.exe"
# Download git installer
Invoke-WebRequest -Uri https://github.com/git-for-windows/git/releases/download/v2.37.1.windows.1/Git-2.37.1-64-bit.exe -UseBasicParsing -OutFile $exePath
# Execute git installer
Start-Process $exePath -ArgumentList '/VERYSILENT /NORESTART /NOCANCEL /SP- /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS /COMPONENTS="icons,ext\reg\shellhere,assoc,assoc_sh"' -Wait
# Optional: For bash.exe, add 'C:\Program Files\Git\bin' to PATH
[Environment]::SetEnvironmentVariable('Path', "$([Environment]::GetEnvironmentVariable('Path', 'Machine'));C:\Program Files\Git\bin", 'Machine')
# Make new environment variables available in the current PowerShell session:
foreach($level in "Machine","User") {
[Environment]::GetEnvironmentVariables($level).GetEnumerator() | % {
# For Path variables, append the new values, if they're not already in there
if($_.Name -match 'Path$') {
$_.Value = ($((Get-Content "Env:$($_.Name)") + ";$($_.Value)") -split ';' | Select -unique) -join ';'
}
$_
} | Set-Content -Path { "Env:$($_.Name)" }
}
# Work with git
git --version
bash发布于 2022-08-13 15:41:44
# Make new environment variables available in the current PowerShell session:
function reload {
foreach($level in "Machine","User") {
[Environment]::GetEnvironmentVariables($level).GetEnumerator() | % {
# For Path variables, append the new values, if they're not already in there
if($_.Name -match 'Path$') {
$_.Value = ($((Get-Content "Env:$($_.Name)") + ";$($_.Value)") -split ';' | Select -unique) -join ';'
}
$_
} | Set-Content -Path { "Env:$($_.Name)" }
}
}
Write-Host "Installing git..." -ForegroundColor Cyan
$exePath = "$env:TEMP\git.exe"
Invoke-WebRequest -Uri https://github.com/git-for-windows/git/releases/download/v2.37.1.windows.1/Git-2.37.1-64-bit.exe -UseBasicParsing -OutFile $exePath
Start-Process $exePath -ArgumentList '/VERYSILENT /NORESTART /NOCANCEL /SP- /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS /COMPONENTS="icons,ext\reg\shellhere,assoc,assoc_sh"' -Wait
[Environment]::SetEnvironmentVariable('Path', "$([Environment]::GetEnvironmentVariable('Path', 'Machine'));C:\Program Files\Git\bin", 'Machine')
reload
git --version
bash --version发布于 2022-08-09 05:24:38
这不是你问题的确切答案。
由于您不喜欢像WSL这样笨重的东西,所以我有一个很好的替代方案,它也承诺支持本机windows文件系统。使用MSYS2而不是gitbash。这要好得多,git bash最初是基于MSYS2的。
.\msys2-x86_64-latest.exe in --confirm-command --accept-messages --root C:/msys64通过CLI安装它。或者,如果您下载了自解压缩存档,请使用.\msys2-base-x86_64-latest.sfx.exe -y -oC:\安装它。
pacman -Syu更新包列表。pacman -S git安装git你最终会爱上它的。请注意,您在linux中使用的一些键盘快捷键可能无法工作,例如不支持粘贴Ctrl+Shift+v,而且Windows使用Shift+Insert。
https://stackoverflow.com/questions/73285729
复制相似问题