Using Git commands with Service Principal authentication.

Ahwan Mishra 140 Reputation points
2024-09-03T15:38:19.2033333+00:00

I have been granted service principal access to my Azure DevOps repository. Below is the script I'm using:

$resource="499b84ac-1321-427f-aa17-267ca6975798"
$TenantId=" "
$ClientId=" "
$ClientSecret=" "
$TokenUri = "https://login.microsoftonline.com/$TenantID/oauth2/token/" 

$Body = "client_id=$ClientId&client_secret=$ClientSecret&resource=$Resource&grant_type=client_credentials"
$TokenResult = Invoke-RestMethod -Uri $TokenUri -Body $Body -Method "POST"
$AccesToken = $TokenResult.access_token

$URI= "https://$AccesToken@dev.azure.com/Org/Project/_git/Repo"

git clone $URI "C:\Users\DBT\Desktop\git" cd "C:\Users\DBT\Desktop\git" (Get-Item .).FullName $gitCmd = "git -c http.extraheader='AUTHORIZATION: bearer $AccesToken'" Invoke-Expression "$gitCmd add ." Invoke-Expression "$gitCmd commit -m 'Commit message'" Invoke-Expression "$gitCmd push"
 

I'm able to successfully clone the repository, but I'm encountering an issue when trying to commit and push changes. The error message I'm receiving is: git: Author identity unknown.

How can I use service principal authentication to commit and push changes to Azure DevOps/Git?

Azure Role-based access control
Azure Role-based access control
An Azure service that provides fine-grained access management for Azure resources, enabling you to grant users only the rights they need to perform their jobs.
830 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,553 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,596 questions
GitHub Training
GitHub Training
GitHub: A web-based hosting service for software development and version control using Git. Acquired by Microsoft in 2018.Training: Instruction to develop new skills.
47 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Nandan Hegde 32,416 Reputation points MVP
    2024-09-04T11:29:28.2966667+00:00

    According to the error git: Author identity unknown, it seems that you didn't set your account's default identity before trying to commit and push changes. Git requires that a username and email be set for commits because it uses this information to track who has made changes to the repository and record them in history.

    Try below command:

    git config --global user.name "ServicePrincipal"
    git config --global user.email "serviceprincipal@domain.com"
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.