windows - 如何在 PowerShell 上安装 git(在 PowerShell 上使用 bash)?

标签 windows git powershell

https://gitforwindows.org/可以选择将 bash 放入 PowerShell。我需要它,所以不需要安装 WSL 等。我需要在无人值守的情况下安装 git,也就是说,仅使用命令行。现有教程,如 this仅使用 PowerShell 启动安装程序,但我必须使用鼠标来安装东西。

那么,如何使用 PowerShell 在 PowerShell 上安装 git 和 bash?

更新:

我试过了

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

但它卡在“正在安装...”并且不打印任何其他输出。

最佳答案

有两个问题:

  1. Git for Windows不会作为 MSI 包发布。并且您不能仅通过重命名将常规可执行文件转换为 MSI 包。您根本不需要 msiexec.exe。安装程序本身已经具有执行静默安装的参数。只需按原样执行即可:

    $exePath = "$env:TEMP\git.exe"
    Start-Process $exePath -Wait -ArgumentList '/NORESTART /NOCANCEL /SP- /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS /COMPONENTS="icons,ext\reg\shellhere,assoc,assoc_sh" /LOG="C:\git-for-windows.log"'
    

    但是:这仍然会启动一个 GUI。所以你必须添加更多参数才能使安装真正安静。延伸阅读:

    TL;DR:同时添加 /VERYSILENT,您可能希望使用 /LOADINF 来自定义一些设置。

  2. 安装成功后,你会遇到同样的问题,你在类似的问题中已经遇到了,我只是answered .长话短说:

    当前 Process 作用域中的环境变量不会自动更新。通过以下方式手动更新它们:

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

    This code is taken from this answer.

    之后,git --versionGet-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

关于windows - 如何在 PowerShell 上安装 git(在 PowerShell 上使用 bash)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73285729/

相关文章:

windows - 如何为windows和linux电脑设置通用的emacs环境?

java - 如何展现海量数据

php - 适用于Vista的最佳PHP编辑器

使用 SSH 并指定凭据时,Git 推送因公钥失败

git - .gitignore 对空格敏感吗?

azure - 如何使用 ssh-keygen 生成顶部有 ---- BEGIN SSH2 PUBLIC KEY ---- 的公钥?

mfc - "Nobody should be using MFC any more"为什么?

git - 更改 IntelliJ IDEA 的默认 SSH key 路径

powershell - Start-Process Powershell 命令中的动词 RunAs 导致错误

powershell - 如何在Powershell中获取系统上本地用户的纯文本列表/数组?