Git commands fail on Azdo after the new default

Today I ran into the following issue that was announced for new pipelines created after September 2022. They would have the option for shallow fetch enabled for 1 level deep. In effect, this gives you a full copy of the files used to start your pipeline.

Sometimes you want to be able to write something back to your git repository for whatever reason. It will probably look something like this.

git config --global user.email "$(Build.RequestedForEmail)"
git config --global user.name "$(Build.RequestedFor)"
git checkout new-dev

echo "Some content to a file" > .erick 
git add .
git commit -m "add file"
git push

git -c http.extraheader="AUTHORIZATION: bearer $(System.AccessToken)" push --set-upstream origin $(Build.SourceBranchName)

When you do this in a pipeline created after September 2022 you will get the following error. Due to the new default shallow fetch, there are no git resource files available, and you are effectively in a detached head state.

The fix is easy to achieve, just add them fetchDept: 0 to the checkout in your pipeline. This will force the repo to be fetched like before, with all the git artifacts that you need for your git commands.

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

Let’s hope this blog saves somebody the pain we had and had to figure out what happened overnight 😉

Share

You may also like...

Leave a Reply