Home > other >  How can I install git (with bash on PowerShell) on PowerShell?
How can I install git (with bash on PowerShell) on PowerShell?

Time:08-12

https://gitforwindows.org/ has an option to put bash into PowerShell. I need that so no installing WSL and etc. I need to install git unattended, that is, with command line only. Existing tutorials like this only launch the installer using PowerShell, but I have to use the mouse to install stuff.

So, how can I install git, with bash on PowerShell, using PowerShell?

UPDATE:

I tried

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

but it gets stuck on "Installing..." and does not print any other outputs.

CodePudding user response:

There are two problems:

  1. Git for Windows does not get released as an MSI package. And you cannot convert a regular executable into an MSI package just by renaming it. You do not need msiexec.exe at all. The installer itself has already paramaters to perform a silent installation. Just execute it as is:

    $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"'
    

    But: This will sill launch a GUI. So you have to add more parameters to make the installation really silent. Further reading:

    TL;DR: Also add /VERYSILENT and you might want to use /LOADINF to customize some settings.

  2. After the successful installation, you will face the same problem, you already did in your similar question, I just answered. TL;DR:

    The environment variables in your current Process scope are not updated automatically. Update them manually by:

    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.

    After that, git --version and Get-Command git will work.

CodePudding user response:

This is not the exact answer for your question.

Since you do not prefer something as heavy as WSL, I have a good alternative for your purpose that also promises native windows filesystem support. Use MSYS2 instead of gitbash. This is far better and git-bash is originally based on MSYS2

  1. Download the prefered package of MSYS2.
  2. If you downloaded the GUI installer, install it via CLI with .\msys2-x86_64-latest.exe in --confirm-command --accept-messages --root C:/msys64

Or if you had downloaded the self extracting archive, install it using .\msys2-base-x86_64-latest.sfx.exe -y -oC:\

  1. Lauch MSYS2, then update the packages list with pacman -Syu.

  2. Install git with pacman -S git

You will eventually come to love it. Note that some keyboard shortcuts you are used to in linux may not work example Ctrl Shift v for pasting is not supported and Windows uses Shift Insert

Credits

CodePudding user response:

Just in case, check if the /LOG="C:git-for-windows.log" part of your command has a typo

 /LOG="C:\git-for-windows.log"
        ^^^ 
        (\ was missing)

That way, you can try again, and monitor C:\git-for-windows.log for logs.

Also, make sure you have the right to write directly under C:\.
A /LOG="$env:userprofile\git-for-windows.log" might be safer.

  • Related