Azure pipeline: can't push a file to an already-existing Azure repo branch

I'm trying to write an Azure pipeline that:

  • runs a python ( script that queries an API and returns a json file (myJson.json)
  • pushes this json file to a specific branch (DIN_grafana_json_archives) in my Azure repo.

I would like this pipeline to run periodically so as to create a series of archived versions of the json file.

The yaml below successfully created my target branch:

trigger: none

  vmImage: ubuntu-latest

- task: UsePythonVersion@0
  displayName: 'Use Python 3.8'
   versionSpec: 3.8

- script: python3 -m pip install --upgrade pip
  displayName: 'upgrade pip'

- script: python3 -m pip install pandas
  displayName: 'install pandas'

- script: python3 -m pip install requests
  displayName: 'install requests'

- script: python3 -m pip install datetime
  displayName: 'install datetime'

- task: PythonScript@0
    scriptSource: 'filePath'
    scriptPath: '$(System.DefaultWorkingDirectory)/'

- bash: |
   git config --global {myUsername}
   git config --global {[email protected]}
   git switch --orphan "DIN_grafana_json_archives"
   git add myJson.json
   git commit -m "add file myJson.json"

   git push --set-upstream https://{myPersonalAccessToken} DIN_grafana_json_archives

  displayName: 'push json file to repo'

At this stage, my branch DIN_grafana_json_archives is successfully created and contains the json file I pushed to it.

Now I'd like to run the pipeline again, that is, to retrieve a new version of the same json file and add it to the DIN_grafana_json_archives branch. Here's my edited yaml file:

# previous steps of the pipeline remain unchanged
- bash: |
   git config --global {myUsername}
   git config --global {[email protected]}
   git checkout DIN_grafana_json_archives
   git add myJson.json
   git commit -m "add file myJson.json"

   git push https://{myPersonalAccessToken}  DIN_grafana_json_archives

Upon running, this bash step fails with the following error:

Starting: Bash
Task         : Bash
Description  : Run a Bash script on macOS, Linux, or Windows
Version      : 3.231.0
Author       : Microsoft Corporation
Help         :
Generating script.
========================== Starting Command Output ===========================
/usr/bin/bash /home/vsts/work/_temp/
error: pathspec 'DIN_grafana_json_archives' did not match any file(s) known to git
[detached HEAD fecac33] add file myJson.json
 1 file changed, 285 insertions(+)
 create mode 100644 myJson.json
error: src refspec DIN_grafana_json_archives does not match any
error: failed to push some refs to ''
##[error]Bash exited with code '1'.
Finishing: Bash

I've tried looking into

  • what "detached HEAD" means
  • the "pathspec did not match any file" error and experimented with pulling/fetching the repo to the Azure VM before the push, but I keep getting the same error message at some stage.

I'm out of my depth and I'd like someone to help solve this issue. It would also be amazing if you could provide some explanation so I can better understand what exactly is not working here. Thank you so much!


  • If the branch DIN_grafana_json_archives has been existing in the remote repository, and a myJson.json file also is existing in the remote DIN_grafana_json_archives branch.

    Now, if you want to update the content of myJson.json file and push the updates to the remote DIN_grafana_json_archives branch, you can do like as below:

    1. Go to Project Settings > Repositories > Security tab, ensure the following two identities at least have the Contribute permission set to Allow.

      • {project name} Build Service ({organization name})
      • Project Collection Build Service ({organization name})

      enter image description here

    2. Configure your pipeline like as below.

        # Check out source files and persist the Git Credentials for use in next steps.
        - checkout: self
          persistCredentials: true
        # Check out 'DIN_grafana_json_archives' branch from remote.
        - task: Bash@3
          displayName: 'Check out branch DIN_grafana_json_archives'
            targetType: inline
            script: git checkout -b DIN_grafana_json_archives origin/DIN_grafana_json_archives
        # Generate a new 'myJson.json' file with the new content in the Python script. 
        - task: PythonScript@0
            scriptSource: 'filePath'
            scriptPath: '$(System.DefaultWorkingDirectory)/'
        # Commit and push the updated 'myJson.json' file to remote 'DIN_grafana_json_archives' branch.
        - task: Bash@3
          displayName: 'Push updates to remote'
            targetType: inline
            script: |
              git config --global {username}
              git config --global {email}
              git add myJson.json
              git commit -m "Update file myJson.json"
              git push

    The command you are using,

    git checkout DIN_grafana_json_archives

    it just creates a new local branch with the name 'DIN_grafana_json_archives'. It will not automatically track with the existing remote 'DIN_grafana_json_archives' branch. This should be the reason of the error you get.

    The command I shared above,

    git checkout -b DIN_grafana_json_archives origin/DIN_grafana_json_archives


    git checkout -b DIN_grafana_json_archives --track origin/DIN_grafana_json_archives

    it will create the local branch (DIN_grafana_json_archives) and let it track with the existing remote branch (origin/DIN_grafana_json_archives).

    Related documentations: