Search code examples
gitpowershellgit-checkout

Is there a way to checkout latest version of origin branch without warnings


I wrote a powershell script to obtain git submodule and set his branch. It will run from pipeline and branch name will be passed from launch form input.

Param(
    [Parameter(Mandatory=$false)]
    [string]$branch = ""
)

cd "$PSScriptRoot\.."

git submodule update --init --recursive

Write-Host "Got branch $branch"

if (-not [string]::IsNullOrWhiteSpace($branch)) {
    cd "submodule"

    git checkout "origin/$branch"
    Write-Host "Installed branch $branch"
} else {
    Write-Host "Installed default branch"
}

The problem is with git checkout "origin/$branch" it does what I need, but shows a warning. Is there a more correct way to achieve what it does?


Solution

  • The warning about detached HEAD state? The warning itself tells you what to do to avoid it from showing up.

    Turn off this advice by setting config variable advice.detachedHead to false
    

    So, something like this should suffice:

    git -c advice.detachedHead=false checkout "origin/$branch"