DevOps  

Git Commands to Push a Local Project to an Azure DevOps Repository

Introduction

When we develop an application locally, we usually want to store the source code in a remote repository so that it can be shared, versioned, and backed up. One common platform used for this purpose is Azure DevOps.

This article explains the basic Git commands used to initialize a repository, track files, commit changes, and push the code to Azure DevOps.

1. Initialize a Git Repository

git init

This command initializes a new Git repository in the current project folder.

  • It creates a hidden .git directory.

  • Git starts tracking the changes in that folder.

Example:

If you created a new project folder for your application, run this command inside that folder to enable Git version control.

2. Check Repository Status

git status

This command shows the current state of the repository.

It displays:

  • Files that are not tracked

  • Files that are staged

  • Files that are modified

This helps developers understand what changes are ready to be committed.

3. Add Files to the Staging Area

git add .

This command adds all files in the project folder to the staging area.

The staging area is like a preparation step before committing changes.

Other examples:

Add a specific file:

git add filename.cs

Add all files:

git add .

4. Commit the Changes

git commit -m "Initial commit"

A commit saves a snapshot of the staged changes into the Git repository.

-m is used to add a commit message explaining the changes.

Example message:

  • Initial commit

  • Added project structure and configuration files

5. Add a Remote Repository

git remote add origin https://dev.azure.com/dtools/_git/DTData

This command connects the local repository to a remote repository hosted in Azure DevOps.

Explanation:

  • origin → Name of the remote repository

  • URL → The repository address in Azure DevOps

After this step, Git knows where to push the code.

6. Push Code to the Remote Repository

git push -u origin main

This command uploads the committed code to the remote repository.

Explanation:

  • push → Upload local commits

  • origin → Remote repository

  • main → Branch name

  • -u → Sets the upstream branch so future pushes can be done with git push

After this step, your project will appear in the Azure DevOps repository.

7. Clone an Existing Repository

git clone https://[email protected]/dtools/DTData/_git/DTData

This command downloads an existing repository from Azure DevOps to your local machine.

After cloning, a folder named DTData will be created.

8. Navigate to the Project Folder

cd DTData

This command moves into the cloned project directory.

All further Git operations should be executed inside this folder.

9. Reinitializing and Connecting the Repository

Sometimes developers may run the following commands again when configuring Git:

git init
git remote add origin https://[email protected]/dtools/DTData/_git/DTData
git add .
git commit -m "Initial commit"
git push -u origin main

This sequence:

  • Initializes Git

  • Adds Azure DevOps repository as remote

  • Stages files

  • Creates a commit

  • Pushes code to the remote repository

Summary

The typical workflow for pushing code to Azure DevOps using Git is:

  • Initialize repository

  • Add files

  • Commit changes

  • Connect to remote repository

  • Push code

Basic flow:

git init
git add .
git commit -m "Initial commit"
git remote add origin <repository-url>
git push -u origin main

This workflow helps developers maintain version control, collaboration, and backup of source code.