I faced the same issue with a newly created repository/project.
I have a script with is updating the package version for an angular app.
With an repo which was created somewhen in spring.
Its checking out code with the real branch name and then creating on one the hash.
* [new branch] main -> origin/main
git --config-env=http.extraheader=env_var_http.extraheader fetch --force --tags --prune --prune-tags --progress --no-recurse-submodules origin +67169f67e1151398ee1c86c939f9aea8daad0a46
From https://xxx.visualstudio.com/xxx/_git/xxx
* branch 67169f67e1151398ee1c86c939f9aea8daad0a46 -> FETCH_HEAD
git checkout --progress --force 67169f67e1151398ee1c86c939f9aea8daad0a46
Note: switching to '67169f67e1151398ee1c86c939f9aea8daad0a46
With that is was able to use $Env:BUILD_SOURCEBRANCH and $Env:BUILD_SOURCEBRANCHNAME
Write-Host "upgrade version"
npm version patch -m "Upgrade to %s ***NO_CI***"
Write-Host "Create temp branch"
git branch tmp
Write-Host "Checkout $SourceBranchPath"
git checkout $Env:BUILD_SOURCEBRANCHNAME -q
Write-Host "Merge tmp to $Env:BUILD_SOURCEBRANCHNAME"
git merge tmp
Write-Host "Update status"
git status
Write-Host "Push changes to origin"
git push origin $Env:BUILD_SOURCEBRANCHNAME -q
Write-Host "Delete tmp branch"
git branch -d tmp -q
But now with the new repo its using the hash at more places.
* [new ref] 8c41292bd04b87275886d7d012c022273ce83f34 -> origin/8c41292bd04b87275886d7d012c022273ce83f34
git --config-env=http.extraheader=env_var_http.extraheader fetch --force --tags --prune --prune-tags --progress --no-recurse-submodules origin --depth=1 +8c41292bd04b87275886d7d012c022273ce83f34
From https://xxx.visualstudio.com/xxx/_git/xxx
* branch 8c41292bd04b87275886d7d012c022273ce83f34 -> FETCH_HEAD
git checkout --progress --force refs/remotes/origin/8c41292bd04b87275886d7d012c022273ce83f34
Note: switching to 'refs/remotes/origin/8c41292bd04b87275886d7d012c022273ce83f34'
I could get rid of error: pathspec
by using $Env:BUILD_SOURCEVERSION
for the checkout.
git checkout $Env:BUILD_SOURCEVERSION -q
But neither $Env:BUILD_SOURCEVERSION
nor $Env:BUILD_SOURCEBRANCHNAME
is working for the final push.
There i get:
src refspec main does not match any
error: failed to push some refs to 'xxxx'