首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在PowerShell上安装git (在PowerShell上使用bash )?

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

Stack Overflow用户
提问于 2022-08-09 01:51:36
回答 5查看 762关注 0票数 2

https://gitforwindows.org/可以选择将bash放入PowerShell中。我需要这样做,所以没有安装WSL等。我需要安装git无人值守,也就是说,只有命令行。像这样的现有教程只使用PowerShell启动安装程序,但是我必须使用鼠标来安装东西。

那么,如何使用PowerShell使用PowerShell来安装git呢?

更新:

我试过了

代码语言:javascript
复制
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

但它被困在“安装.”也不打印任何其他输出。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2022-08-11 06:59:24

有两个问题:

  1. 用于Windows的Git不作为MSI包发布。而且,您不能仅仅通过重命名常规可执行文件来将其转换为MSI包。您根本不需要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。因此,您必须添加更多的参数,以使安装真正安静。进一步读:
代码语言:javascript
复制
- [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来自定义一些设置。

  1. 安装成功后,您将面临同样的问题,您已经在您的类似问题上做了,我只是已应答。TL;DR: 当前Process作用域中的环境变量不会自动更新。通过以下方式手动更新: foreach(在“机器”、“用户”中的$level){$level%{#表示路径变量,如果它们还没有在其中,如果($_.Name -match ' Path $') { $_.Value =((Get-Content "Env:$($_.Name)") +“;$($_.Value) -split ';‘Select’-join ';{ "Env:$($_.Name)“}} 这段代码取自这个答案

在此之后,git --versionGet-Command git将工作。

完整脚本:

代码语言:javascript
复制
$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
票数 3
EN

Stack Overflow用户

发布于 2022-08-13 15:41:44

代码语言:javascript
复制
# 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
票数 2
EN

Stack Overflow用户

发布于 2022-08-09 05:24:38

这不是你问题的确切答案。

由于您不喜欢像WSL这样笨重的东西,所以我有一个很好的替代方案,它也承诺支持本机windows文件系统。使用MSYS2而不是gitbash。这要好得多,git bash最初是基于MSYS2的。

  1. 下载首选的MSYS2包。
  2. 如果下载了GUI安装程序,请使用.\msys2-x86_64-latest.exe in --confirm-command --accept-messages --root C:/msys64通过CLI安装它。

或者,如果您下载了自解压缩存档,请使用.\msys2-base-x86_64-latest.sfx.exe -y -oC:\安装它。

  1. Lauch MSYS2,然后用pacman -Syu更新包列表。
  2. pacman -S git安装git

你最终会爱上它的。请注意,您在linux中使用的一些键盘快捷键可能无法工作,例如不支持粘贴Ctrl+Shift+v,而且Windows使用Shift+Insert。

学分

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73285729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档