Reader small image

You're reading from  Hands-On Visual Studio 2022 - Second Edition

Product typeBook
Published inMar 2024
PublisherPackt
ISBN-139781835080443
Edition2nd Edition
Right arrow
Authors (2):
Hector Uriel Perez Rojas
Hector Uriel Perez Rojas
author image
Hector Uriel Perez Rojas

Hector is an experienced senior developer, with more than 10 years of experience in developing desktop, web, and mobile solutions with the .NET platform. He was recognized with the Microsoft MVP award for two consecutive years in 2021 and 2022. He is an active member of the .NET community and is the founder of El Camino Dev and Devs School academies.
Read more about Hector Uriel Perez Rojas

Miguel Angel Teheran Garcia
Miguel Angel Teheran Garcia
author image
Miguel Angel Teheran Garcia

Miguel Angel Teheran Garcia is a solutions Architect and Technical Lead recognized as a Microsoft MVP, C# corner MVP, and Alibaba Cloud MVP. He is a member of the Avanet community in Medellín and an active member of tech events in Colombia. He has also been a speaker at different conferences around Latin America.
Read more about Miguel Angel Teheran Garcia

View More author details
Right arrow

Implementing Git Integration

Having a change control platform for the development of a software project is essential for good control of the project. There are many different versioning systems, but Git is the most widely used system today, which is why more and more IDEs are including tools for managing repositories based on this technology natively.

This is precisely what happened with VS, which integrates a series of options to allow us to work with Git-based repositories.

In this chapter, you are going to learn how to work with Git repositories based on GitHub, which is the most popular repository-hosting platform today.

The main topics we will see in the chapter are as follows:

  • Getting started with Git settings
  • Creating a Git repository
  • Cloning a Git repository
  • Fetching, pulling, and pushing Git repositories
  • Managing branches
  • Viewing changes in repositories

Technical requirements

To follow along with this chapter, you must have installed VS with the workload set from Chapter 1, Getting Started with Visual Studio 2022.

Since the projects hosted in the main repository of the previous chapters already have a GitHub configuration, we will create a new project throughout the chapter to perform the practices. Therefore, a GitHub account is required, which can be created at https://github.com/signup.

Getting started with Git settings

Starting to work with Git tools is very easy in VS 2022 since they are included as part of the installation itself, so you can install VS and start working on your projects as soon as possible.

To access the management of code projects hosted on GitHub, you must first sign in with a Microsoft account, as explained in Chapter 1, Getting Started with Visual Studio 2022. Once logged in, click on the account profile icon and select the Account settings option, as shown in Figure 9.1:

Figure 9.1 – Accessing the account settings

Figure 9.1 – Accessing the account settings

This will open an account customization window, where we can add a GitHub account, via the +Add button, as shown in Figure 9.2:

Figure 9.2 – The “+Add” button for adding a GitHub account

Figure 9.2 – The “+Add” button for adding a GitHub account

Once we press the button, we will be redirected to the GitHub authentication portal, where we can log in with an existing GitHub account or create an account...

Creating a Git repository

Creating a repository in GitHub from a VS project is very easy to do. In this section, you will have to test your knowledge by creating a new ASP.NET Core Empty project, as discussed in the Templates for ASP.NET Core web applications section of Chapter 2, Creating Projects and Templates. You can name this project GitDemo.

To create the new repository in GitHub from the project you have created, just select the Git | Create a Git Repository menu option and fill in the repository information, according to the data shown in Figure 9.5:

Figure 9.5 – Filling in the information for a new repository on GitHub

Figure 9.5 – Filling in the information for a new repository on GitHub

Let’s briefly look at each option:

  • Local path: This sets the path on the local machine where the source code is hosted (usually the path where you created the project).
  • .gitignore template: This allows you to select a template that establishes a set of files that will not be uploaded to the repository...

Cloning a Git repository

You may want to clone an existing repository and not start from scratch because you need to work in a team. Alternatively, you may simply have come across a repository that caught your attention while browsing the GitHub site.

The easiest way to clone a repository is from the initial window of VS, which can be reached by either starting a new instance of VS, closing an open project, or going to File | Start Window. In this window, the first option is the Clone a repository button:

Figure 9.9 – The “Clone a repository” option from the startup window

Figure 9.9 – The “Clone a repository” option from the startup window

Once we press this button, a new window (shown in Figure 9.10) will open, asking us to indicate the URL of the remote repository and the local path where the source code files will be stored:

Figure 9.10 – The window for cloning a repository

Figure 9.10 – The window for cloning a repository

For the purposes of this demonstration, we will use the URL of the repository created...

Fetching, pulling, and pushing Git repositories

The most important commands when working with Git repositories have to do with fetching, pulling, and pushing operations. There are two main ways to execute these operations:

  • The first way is by accessing them through the Git menu, as shown in Figure 9.12:
Figure 9.12 – Accessing the Fetch, Pull, and Push operations from the menu

Figure 9.12 – Accessing the Fetch, Pull, and Push operations from the menu

  • The second way is to enable the Git Changes window, which you can open through the View | Git Changes menu, as shown in Figure 9.13:
Figure 9.13 – Accessing the Fetch, Pull, and Push operations from the Git Changes window

Figure 9.13 – Accessing the Fetch, Pull, and Push operations from the Git Changes window

At this point, you may be wondering what each of these operations is for. So, let’s learn about them briefly.

Fetching repositories

The fetch operation allows you to check whether there are remote commits that should be incorporated into the local repository.

To run this example, go...

Managing branches

So far, we have been working with the main branch of our project called master. Imagine this branch as a timeline, where each event is performed by a commit. This is very useful when there is some conflict and you need to go back to a previous version, undoing the changes of a specific commit.

However, if you are working in a team, it is common that you will need to add a functionality in some kind of sandbox before merging this functionality into the master branch. It is in this sort of scenario that Git branches will help us, allowing us to create a new project branch from an existing repository branch and work on it without affecting the functionality of the main repository.

To create a new branch, just go to the Git | New Branch. This will open a new window that asks for the branch name—the branch on which the new branch will be based— and a checkbox labeled Checkout branch, which, if checked, will transition to the new branch once it is created...

Viewing changes in repositories

There are several ways in which VS helps us to visualize the changes in the repositories. The first one is through the Git Repository window (which, in case you accidentally closed it, can be found in the View menu), as shown in Figure 9.27:

Figure 9.27 – The Git Repository window

Figure 9.27 – The Git Repository window

The Git Repository window displays Incoming, which allows us to visualize whether there are versions that have not been applied in our local repository through, Outgoing, which shows whether there are commits made locally but not pushed to the server, and also Local History.

If we want to visualize the changes that have occurred between different commits, just right-click on two or more commits and select the Compare Commits… option:

Figure 9.28 – The "Compare Commits…" option

Figure 9.28 – The "Compare Commits…" option

This will display a new window with the changes that occurred between the different source files.

...

Linking GitHub issues

A new feature that has been added to VS 2022 is the ability to link commits to a specific issue. Let’s see how to do it in a practical way. For this practice, go to the GitHub repository you have created in the Creating a Git repository section. Let’s test this feature with a step-by-step example:

  1. Once you have opened the repository, click on the Issues option:
Figure 9.30 – The "Issues" section of the repository

Figure 9.30 – The "Issues" section of the repository

  1. Next, press the button on the right side labeled New issue:
Figure 9.31 – Button to add a new issue

Figure 9.31 – Button to add a new issue

  1. After clicking on the New issue button, we will be redirected to a new window, as shown in Figure 9.32, where we will raise an issue to indicate that there is a misspelled text. For it I will set the Title to: Typographical Error in Root Endpoint Response. In the Extended description text box, I will add the following comment in markdown...

Summary

In this chapter, we have learned how VS integrates tools so that we can easily manage projects using Git and GitHub.

Knowing how to work with Git-based projects is indispensable for all developers who want to manage their projects in a more controlled way, while having an overview of a project structure in previous versions.

Likewise, if you work with other developers, you will be able to share project tasks, test them independently, and merge them when the code is reliable in order to not damage the source code that has already been tested.

That is why we have learned how to set up a GitHub account in VS, how to create and clone repositories, how to perform fetch, pull, and push operations, how to manage branches in our projects, how to visualize changes in repositories, and, finally, how to reference repository issues with a commit.

In Chapter 10, Sharing Code with Live Share, you will learn how to work collaboratively with a development team on the same project...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Visual Studio 2022 - Second Edition
Published in: Mar 2024Publisher: PacktISBN-13: 9781835080443
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Hector Uriel Perez Rojas

Hector is an experienced senior developer, with more than 10 years of experience in developing desktop, web, and mobile solutions with the .NET platform. He was recognized with the Microsoft MVP award for two consecutive years in 2021 and 2022. He is an active member of the .NET community and is the founder of El Camino Dev and Devs School academies.
Read more about Hector Uriel Perez Rojas

author image
Miguel Angel Teheran Garcia

Miguel Angel Teheran Garcia is a solutions Architect and Technical Lead recognized as a Microsoft MVP, C# corner MVP, and Alibaba Cloud MVP. He is a member of the Avanet community in Medellín and an active member of tech events in Colombia. He has also been a speaker at different conferences around Latin America.
Read more about Miguel Angel Teheran Garcia