BendEg
BendEg

Reputation: 21088

Azure DevOps throws error: pathspec 'master' did not match any file(s) known to git

We have about 100 running azure-devop pipelines with all the same azure-pipeline.yaml. In all pipelines, we are calling get checkout $(Build.SourceBranchName). In one of the repositories we get the error message:

error: pathspec 'master' did not match any file(s) known to git

On our local machines checking out the master branch does not make any problems. When pushing to any other branch, it is not working as well. E.g. when using dev branch:

error: pathspec 'dev' did not match any file(s) known to git

I can't see any differences. What I tried:

Any other ideas?

Upvotes: 3

Views: 3263

Answers (6)

AditYa
AditYa

Reputation: 897

I had the same issue and resolved it by using the below PowerShell@2 task in my Yaml file.

  script: |
               $env:AZDO_PERSONAL_ACCESS_TOKEN
               $env:AZDO_ORG_SERVICE_URL="https://dev.azure.com/<Org_name>"
               git config --global http.https://<Org_name>@dev.azure.com.extraheader "AUTHORIZATION: bearer $env:AZDO_PERSONAL_ACCESS_TOKEN"

env:
    AZDO_PERSONAL_ACCESS_TOKEN: $(System.AccessToken)

The issue was with the env variable names, which only work with AZDO_PERSONAL_ACCESS_TOKEN and AZDO_ORG_SERVICE_URL.

And when we are not able to config the git properly it throws the Error (error pathspec master did not match any files known to)

Upvotes: 0

WizardOfAuz
WizardOfAuz

Reputation: 106

I stumbled across this same issue recently where exactly the same pipeline script was working on an existing project, but not a new one I was setting up. It turns out that the default behaviour of the checkout step changed in September 2022 but any pre-existing pipelines would retain the old behaviour.

Screenshot from Microsoft's steps.checkout docs

To get the pre-September 2022 behaviour back you need to change from this:

steps:
- checkout: self
  persistCredentials: true

to this:

steps:
- checkout: self
  fetchDepth: 0
  fetchTags: true
  persistCredentials: true

Once you've done that you'll be able to access your repo's branches and tags. e.g.

steps:
- checkout: self
  fetchDepth: 0
  fetchTags: true
  persistCredentials: true

- script: |
    git config --global user.email '[email protected]'
    git config --global user.name 'AzDO Pipeline'
    git checkout $(Build.SourceBranchName)
  displayName: 'Set up git'

- script: |
    pip install bump2version
    bump2version --verbose ${{ parameters.RELEASE_TYPE }}
  displayName: 'Bump version'

- script: |
    git push origin --tags
    git push origin $(Build.SourceBranchName)
  displayName: 'Push tagged release'

Upvotes: 5

Vermaat
Vermaat

Reputation: 11

I had the same problem with a new repo, while the same script worked in an old repo in the same devops project. Not really sure why.

For me the solution was to run git fetch before the checkout.

Upvotes: 1

Robert
Robert

Reputation: 11

Meanwhile I got it working.

As it seems to be aware of the current branch its running on, you have to create this branch with
git checkout -b $Env:BUILD_SOURCEBRANCHNAME <- -b

Later on to push back the changes, you have to use
git push origin HEAD:$Env:BUILD_SOURCEBRANCHNAME<- HEAD:

Upvotes: 0

Robert
Robert

Reputation: 11

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'

Upvotes: 1

VonC
VonC

Reputation: 1323035

I would check in that pipeline:

  • git version
  • git branch -avv
  • git switch $(Build.SourceBranchName)

The last command (git switch) would avoid any issue where you checkout a branch which could also be a file name.

Upvotes: 1

Related Questions