Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
strauss_d_getting_started_with_visual_studio_2022_learning_a.pdf
Скачиваний:
124
Добавлен:
26.06.2023
Размер:
17.04 Mб
Скачать

Chapter 5 Source Control

Figure 5-24.  The Team Explorer window

I now have cloned the repository to my local machine, and I can now collaborate with @acsharpdev on his project and share my changes with him easily.

Create a Branch from Your Code

A new feature needs to be added to the Pet project. It would be better for me to work on the changes to the project in an isolated manner. To do this, I can create a branch in Git. A branch allows me to make changes to the code without changing the code in the main branch, also called the master branch. In Visual Studio, I can see that I am currently working on the master branch if I look at the bottom-right status bar in Visual Studio (Figure 5-25).

Figure 5-25.  Working in the master branch

268

Chapter 5 Source Control

To create a new branch, click the current branch to open the Branches view (Figure 5-26).

Figure 5-26.  Branches view

I will now create a local branch in Visual Studio. To do this, click the New Branch button in the Branches view.

Figure 5-27.  Create a new branch

269

Chapter 5 Source Control

I can now give my new branch a suitable name (Figure 5-27) and tell it to create the branch from the master branch. I keep the Checkout branch selected to check out my new branch and click the Create button.

Figure 5-28.  Feature branch created

As seen in Figure 5-28, my new local branch is created and checked out. When I look at the bottom-right status bar in Visual Studio, I see that the new feature branch is checked out (Figure 5-29).

Figure 5-29.  Feature branch checked out

270

Chapter 5 Source Control

This means that from now on, all changes made to the code will stay in this branch. Let’s add some new code to the project.

Figure 5-30.  New feature code added

As seen in Figure 5-30, I have added a new class called CoolFeatureClass that contains the new code I added. I must now commit the changes to my branch. In the Git Changes tab, I can see the code that I have changed in my branch (Figure 5-31).

271

Chapter 5 Source Control

Figure 5-31.  Changes to feature branch

You can see that the SuperCoolFeature branch is still selected. Under the changes section, you will see all the files that I have changed. Before you commit your code, you need to add a suitable commit message. Then I can click the drop-down next to the Commit All button and select Commit All and Push (Figure 5-32).

272

Chapter 5 Source Control

Figure 5-32.  Commit All and Push

This will commit the changes to the local repo and then push them to the remote repo. If you click the SuperCoolFeature branch and view the Remotes tab, you will see that your feature branch has been pushed to GitHub (Figure 5-33).

273