I am trying to code up a condition to check if a git repo has a specific branch. If not, then create that branch.
I know the following commands via command line can be used to achieve this:
git branch
git checkout etc..
But I was wondering if there are any Azure Command line extensions to do this?
You can try az repos ref list. Use the filter option to search branches like "heads/your_path" (example on rest API documentation)
Example:
az devops login --org "https://dev.azure.com/<your_org>"
Token:
az repos ref list --org "https://dev.azure.com/your_org" -p TeamProject -r RepoName --filter heads/
[
{
"creator": {
"descriptor": "------",
"directoryAlias": null,
"displayName": "------",
"id": "0f98bf70-30c3----",
"imageUrl": "----",
"inactive": null,
"isAadIdentity": null,
"isContainer": null,
"isDeletedInOrigin": null,
"profileUrl": null,
"uniqueName": "-------",
"url": "-------"
},
"isLocked": null,
"isLockedBy": null,
"name": "refs/heads/dev",
"objectId": "4734085d73703e88ae39-----",
"peeledObjectId": null,
"statuses": null,
"url": "-----"
},
Thank you. This really helps. I can get the branch names using this. Do you know if I can create a fresh branch using this as well? For example: I see this command. az repos ref create. I am not a git expert and I am confused what the refs/heads/ is before the name of a branch. Would I still have to provide refs/heads while creating branch? Can you give an example for that as well? Thank you
@Rishabh to create a branch, you have to know the base object id. As an example, create a new branch based on master. Find the master branch with
az repos ref list
and the filterheads/master
. Then useaz repos ref create
with a new name (like 'heads/dev2') andobjectId
from the master branch.Thank you very much.
I am creating a git repo in ADO using the 'az repos create' command and I am trying to read the base object id. But it doesn't exist until the master branch exists. How do I create the repo and at the same time initialize it with a README.md or something? Can this be done without a git add, commit and push?
The Rest API tries to create initial commits through pushes: docs.microsoft.com/en-us/rest/api/azure/devops/git/pushes/…. But it does not exist in az cli. Try to set object id with "0000000000000000000000000000000000000000" like in the rest API example.