Home > Software design >  PowerShell, git pull if present, or git clone if not
PowerShell, git pull if present, or git clone if not

Time:10-20

I saw this construct on a question about bash

git -C 'repo-path' pull || git clone https://server/repo-name 'repo-path'

So, if the git pull fails, then do the git clone, which seems quite a clean approach.

What would be an equivalent syntax for this in PowerShell?

CodePudding user response:

I believe that syntax would work as expected on PowerShell core, since it now supports || and &&.

If you need this to work on pre-PowerShell core systems, you could use $lastExitCode to determine if the previous step worked or not.

git -C 'repo-path' pull
# If the previous line worked, lastExitCode will be zero
if ($lastexitCode) {  
   git clone https://server/repo-name 'repo-path'
}
  • Related