Friday, August 1, 2025

Sample .NET Core project and pushing it to an Azure Repos repository

 

Creating a sample .NET Core project and pushing it to an Azure Repos repository is a fundamental step in setting up a modern development workflow. Here's a detailed, step-by-step guide on how to do it.

Prerequisites

  • An Azure DevOps account and a project.

  • Git installed on your local machine.

  • The .NET Core SDK installed on your local machine.

  • Visual Studio or Visual Studio Code (recommended for a smoother experience).


Step 1: Create the .NET Core Project Locally

You can create a new .NET Core project using the command line. This is a quick and clean way to get started.

  1. Open a command prompt or terminal.

  2. Navigate to a directory where you want to create your project. For example: cd C:\Projects

  3. Create a new .NET Core project. You can choose from various templates (e.g., webapi, mvc, console). For this example, let's create a simple web API.

    • dotnet new webapi -n MySampleApp

  4. Navigate into your new project directory.

    • cd MySampleApp

Your local project is now created and ready for version control.


Step 2: Initialize a Local Git Repository

Before you can push your code to Azure Repos, you need to turn your local project directory into a Git repository.

  1. Initialize the local Git repository.

    • git init

  2. Add all the project files to the staging area.

    • git add .

  3. Commit the files to your local repository. This creates your first commit.

    • git commit -m "Initial commit of a .NET Core web API project"


Step 3: Create the Repository in Azure Repos

Now you need a remote repository in Azure DevOps to push your local code to.

  1. Sign in to your Azure DevOps account (e.g., https://dev.azure.com/{your-organization}).

  2. Navigate to your project.

  3. In the left-hand navigation menu, select Repos.

  4. If it's your first time, you'll be prompted to create a new repository. Click the "New repository" button. If you already have one, click the repository dropdown at the top and select "New repository".

  5. Give your repository a name (e.g., MySampleApp).

  6. You can choose to add a README.md or a .gitignore file. For a .NET Core project, it's a good practice to add a .gitignore from the template dropdown to automatically ignore build artifacts and temporary files. Select the .NET Core template.

  7. Click "Create".

You now have an empty repository in Azure Repos. The next screen will give you instructions on how to connect your local repository.


Step 4: Link Your Local Repo to Azure Repos and Push

This is the final step where you connect your local code to the remote repository you just created.

  1. Copy the remote repository URL.

    • On the new repository page in Azure Repos, you'll see a "Clone" button in the top-right corner. Click it.

    • From the dialog that appears, copy the HTTPS URL. It will look something like this: https://dev.azure.com/{your-organization}/{your-project}/_git/MySampleApp

  2. Add the remote origin to your local repository.

    • Go back to your terminal or command prompt, making sure you are still in your project's root directory (MySampleApp).

    • Run the command to add the remote:

      • git remote add origin https://dev.azure.com/{your-organization}/{your-project}/_git/MySampleApp (Paste the URL you copied from the previous step).

  3. Push your local code to the remote repository.

    • The origin is the alias for your remote repository, and main is the branch you are pushing.

    • git push -u origin main

    • You may be prompted to log in to Azure DevOps. If so, a browser window will open for authentication. If you're using the Git Credential Manager, it will handle this for you.


Verification

Go back to the Azure Repos page in your web browser. Refresh the page. You should now see all the files from your local .NET Core project in your remote repository.

You have successfully created a .NET Core project and pushed it to an Azure Repos repository!

No comments:

Post a Comment