Code CheckIn Using Git Commands

Introduction

When a custom application code is complete and is tested well and final step is to check in to code repository. There are n number of ways to check in the code. I felt using git commands is a simple process and can check in to master branch with less no. of steps. In this article, let's see the steps to have the custom application codes check in to Azure DevOps repository. The steps are outlined for PS script project. The same steps can be used for any custom projects using .Net projects, SPFx projects, etc.

Pre-requisites

  • First, you need to install Git. Please find the references section to install the Git on to your machine.
  • You need to have access to the repository with minimum contribute permissions.

The idea here is not to focus on the PS code, but to get the steps to have the local code pushed to your version control systems, in this is case push to code repo in Azure DevOps.  Below are the steps that will be done at a glance.

Push code to Azure DevOps repository

Accessing the Repo

When given access to Azure DevOps, you should be able to access the repository by following steps

Step 1 - Login to Azure Dev Ops https://dev.azure.com and login with your corporate credentials.

Push code to Azure DevOps repository

Step 2 - Access the workspace, in my case it is ‘Custom Application Codes’

Step 3 - Click on the ‘Repos’ from the quick launch menu.

Push code to Azure DevOps repository

Step 4 - I would like to push my Power Shell Code to this repository. If you observed it there is nothing there in the ‘PSScripts’ repo.

Push code to Azure DevOps repository

From the local folder I have the following file structure which needs to be pushed to Azure Dev Ops Repo

Push code to Azure DevOps repository

Step 5 - Before you would like to push the code, you need to have local repo folder setup which will sync your code files to DevOps repo. I have selected the default repos which is at C:\Users\[UserPRofile]\source\repos

Push code to Azure DevOps repository

Step 6 - Clone the devops repo to this folder using the git commands. For this login to command prompt and change directory to this folder.

Push code to Azure DevOps repository

Step 7 - In the Azure Repos folder, click on the ‘Clone’ and copy the URL.

Push code to Azure DevOps repository

Step 8 - Enter the following command in command prompt

git clone https://vinaya0659@dev.azure.com/vinaya0659/Custom%20Application%20Codes/_git/PSScripts

Push code to Azure DevOps repository

It should ask for authentication in Pop-up

Push code to Azure DevOps repository

Enter your org credentials and on successful authentication, you should see a similar message like below.

Push code to Azure DevOps repository

You could also see local folder called ‘PSScripts’ created in your local repos folder.

Push code to Azure DevOps repository

Step 10 - Change directory to the folder called ‘PSSCripts’. to check the status just enter

git status

Push code to Azure DevOps repository

At this point there is nothing to commit, since we have cloned an empty repository. Next step is to checkin our Powershell Script to the local folder. In this case, I have selected one of the production script called ‘GetADUserAllProperties’ and copied it to local repos folder

Source: My local development folder

Push code to Azure DevOps repository

Destination: My local Repo folder cloned from Azure DevOps

Push code to Azure DevOps repository

Push code to Azure DevOps repository

Step 11 - Now get the status again

Push code to Azure DevOps repository

It says, one of the folder is untracked. In order to add the folder and complete structure, we need to add this to tracker by using following command

git add.

Get status again, now you will see all the green text with new file names which needs to be committed to your Dev Ops Repo.

Push code to Azure DevOps repository

Step 12 - Now commit the code from your local repo to cloud repo. The parameter -m can be of any message according to your preference.

git commit -m “Commiting the final version of code from local repo to cloud devops repo”

Push code to Azure DevOps repository

Step 13 - In this case, I would like to push to main branch since this is the final version of the successfully tested code.

In many organizations, there will be main branch and there will be sub-branches created out of it. Only changes to main branch will be committed by a specific team that has validated the checks and balances on the code. Again each organization's requirements could be different for the source code check-in and version controls. More details on version control for enterprises using git can be found in the references section.

Get the status again

Push code to Azure DevOps repository

Step 14 - You can check the branch to which your code will be pushed. By default, it will be pushed to ‘main’ branch.

Push code to Azure DevOps repository

Step 15 - Finally you can push the code to repo by entering the following command

git push

Push code to Azure DevOps repository

Step 16 - You can check the cloud repo and you should see all the files checked in successfully to main branch.

Push code to Azure DevOps repository

References