Search code examples
gitazure-devopsyamlcontinuous-integrationgit-diff

Resolving git diff error in Azure DevOps pipeline


I'm currently building the CI part of the pipeline. I've created a feature branch:

git branch feature/123
git checkout feature/123

Made some changes to some files...

git add .
git commit -m "changes"

Now I can see the differences using the following command:

git diff HEAD^1 HEAD --name-only

Locally, in my visual studio, it returns the files changed. This is exactly what I want

But, whenever I run the exact same git diff command from my hosted machine in Azure (after checking out the repo, ofcourse). I get the following error:

fatal: ambiguous argument 'HEAD^1': unknown revision or path not in the working tree.

This is what my CI pipeline looks like:

stages:
  - stage: code_checks
    jobs:         
      - job: artifacts_validation_and_requirements
        steps:
          - checkout: 'self'
            submodules: 'true'
            persistCredentials: true
          - script: |
              git diff --name-only --diff-filter=AMR HEAD^1 HEAD 
            displayName: 'Get Changes'

I have no idea why this doesn't work in my CI pipeline but does work in on my local machine.

Could anyone point me in the right direction?

Thanks in advance!


Solution

  • fatal: ambiguous argument 'HEAD^1': unknown revision or path not in the working tree.

    I can reproduce this issue in my pipeline.

    enter image description here

    The cause of the issue could be related to the fetch depth of the Pipeline repo.

    By default, the Shallow fetch of the pipeline repo is 1 by default.

    To solve this issue, you can try to set the fetchDepth to 0 in YAML Pipeline.

    stages:
      - stage: code_checks
        jobs:         
          - job: artifacts_validation_and_requirements
            steps:
              - checkout: 'self'
                submodules: 'true'
                fetchDepth: 0
                persistCredentials: true
              - script: |
                  git diff --name-only --diff-filter=AMR HEAD^1 HEAD 
                displayName: 'Get Changes'