Search code examples
gitpowershellgit-clonegit-pull

PowerShell, git pull if present, or git clone if not


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?


Solution

  • 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'
    }