You are on page 1of 52

Version Control with

GIT in Azure Repos

1
Contents
1 Introduction ........................................................................................................................................................... 3
2 Lab Scenario ........................................................................................................................................................... 4
3 Documentation Links .......................................................................................................................................... 5
4 Pre-Requisites ....................................................................................................................................................... 6
4.1 Set up an Azure DevOps organization ................................................................................................................... 6
5 Install and configure GIT.................................................................................................................................... 7
6 Install & Configure Visual Studio.................................................................................................................. 16
6.1 Install Visual Studio ..................................................................................................................................................16
6.2 Install C# Extension in Visual Studio Code .......................................................................................................20
6.3 Store GIT Credential in Visual Studio Code .......................................................................................................23
7 Clone an Existing Repository ......................................................................................................................... 25
8 Save work with commits ................................................................................................................................. 30
8.1 Commit changes.........................................................................................................................................................30
8.2 Review commits .........................................................................................................................................................34
8.3 Stage changes .............................................................................................................................................................35
9 Review history Of Commits ............................................................................................................................ 39
9.1 Compare Files .............................................................................................................................................................39
10 Working With Branches .............................................................................................................................. 42
10.1 Create a new branch in your local repository...............................................................................................42
10.2 Working with branches .......................................................................................................................................45
11 Summary ........................................................................................................................................................... 52

2
1 INTRODUCTION
Azure DevOps supports two types of version control, GIT and Team Foundation Version
Control (TFVC). Here is a quick overview of the two version control systems:

Team Foundation Version Control (TFVC): TFVC is a centralized version control system.
Typically, team members have only one version of each file on their dev machines. Historical
data is maintained only on the server. Branches are path-based and created on the server.

GIT: Git is a distributed version control system. Git repositories can live locally (such as on a
developer's machine). Each developer has a copy of the source repository on their dev
machine. Developers can commit each set of changes on their dev machine and perform
version control operations such as history and compare without a network connection.

Git is the default version control provider for new projects. You should use Git for version
control in your projects unless you have a specific need for centralized version control
features in TFVC.

3
2 LAB SCENARIO
In this lab, you will learn how to establish a local Git repository, which can easily be
synchronized with a centralized Git repository in Azure DevOps.

In addition, you will learn about Git branching and merging support. You will use Visual
Studio Code, but the same processes apply for using any Git-compatible client.

This Activity Guide cover steps to:

➢ Clone an existing repository

➢ Save work with commits

➢ Review history of changes

➢ Work with branches by using Visual Studio Code

4
3 DOCUMENTATION LINKS

1. Version Control

https://docs.microsoft.com/en-us/devops/develop/git/what-is-version-control

2. What is Git?

https://docs.microsoft.com/en-us/devops/develop/git/what-is-git

3. VS Code

https://code.visualstudio.com/docs

4. MicrosoftLearning/AZ400-DesigningandImplementingMicrosoftDevOpsSolutions
https://github.com/MicrosoftLearning/AZ400-
DesigningandImplementingMicrosoftDevOpsSolutions

5
4 PRE-REQUISITES

4.1 Set up an Azure DevOps


organization
1. You need to have a Active Azure Account (Paid or Free).

Note: If you don’t have Azure Account please check the Activity Guide on “Create Azure
Account & Access Console” under Bonus Module.

2. You need to have a Active Azure DevOps Account.

Note: If you don’t have Azure DevOps Account please Check the Creating FREE Microsoft
Azure DevOps Account” under Module-1.

3. You need to Create PartsUnlimited Team Project (Task 1 only) from Azure DevOps
Generator with having PartsUnlimited as Template.

4. Request for Free Build Pipeline: Follow the Steps from Section 3 of the guide “Creating
FREE Microsoft Azure DevOps Account”

6
5 INSTALL AND CONFIGURE GIT
In this section, we have provided detailed steps to download and install Git on your Windows
Virtual Machine.

1. Create a Windows VM using Azure Portal (https://portal.azure.com/), and to create a Virtual


Machine follow the steps given on this Microsoft Documentation
(https://docs.microsoft.com/en-us/azure/virtual-machines/windows/quick-create-portal)

2. Connect to the Virtual Machine created: https://docs.microsoft.com/en-us/azure/virtual-


machines/windows/quick-create-portal#connect-to-virtual-machine

3. In the VM, open a browser and click on the link to Download Git
https://git-scm.com/download/win

7
4. Run the installer then click on Next.

5. Keep default and click on Next.

8
6. Let the default applied and click on Next.

7. Keep default setting and click on Next.

9
8. Select a text editor like atom, sublime text. I have selected Vim text as it is very light weight
and then click on Next.

9. Make sure Let Git decide is selected then click on Next

10
10. Keep default and click on next

11. Keep default and click on next

11
12. Click on Next

13. Use Default setting and click on Next

12
14. Click on Next

15. Let the default configuration selected and click on Next

13
16. Click on Next

17. Keep default and click on Install

14
18. Click on Finish

Note: Now we have successfully installed Git on Windows VM.

15
6 INSTALL & CONFIGURE VISUAL STUDIO

6.1 Install Visual Studio


Here we will be downloading and installing the Visual Studio Code.

1. In your previously connected Windows VM, Download Visual Studio Code from
https://code.visualstudio.com/ and install on your Windows Virtual Machine.

16
2. Once it is downloaded, run the installer (VSCodeUserSetup-{version}.exe). This will only
take a minute.

3. Accept the agreement and click on Next.

17
4. Click on "create a desktop icon" so that it can be accessed from desktop and click on Next.

5. After that, click on the Install button.

18
6. Finally, after installation completes, click on the Finish button, and the visual studio code will
get open.

After the successful installation, let's move to the next section to understand the various
components of the User Interface of Visual Studio Code Editor.

19
6.2 Install C# Extension in Visual
Studio Code
Here we will be installing the C# extension in our Visual Studio Code.

1. Open Visual Studio Code.

20
2. From the main menu, select Extensions and Type C# and select the first option.

3. Click on Install on the C# Extension Page

21
4. Now you can see that the C# Extension is installed.

22
6.3 Store GIT Credential in Visual
Studio Code
Here we will configure a Git credential helper to securely store the Git credentials used to
communicate with Azure DevOps.
If you have already configured a credential helper and Git identity, you can skip to the next
task.
1. Open Visual Studio Code

23
2. Click on Terminal > Select New Terminal

3. On the Terminal execute the following command below to configure a credential helper.
git config --global credential.helper wincred

4. The commands below will configure your user name and email for Git commits.
Note: Replace the parameters with your preferred user name and email and execute them.
git config --global user.name "Rohit"
git config --global user.email rohit@example.com

24
7 CLONE AN EXISTING REPOSITORY
In this section, you use Visual Studio Code to clone the Git repository you provisioned as part
of the previous exercise.

1. Open your Azure DevOps account and Select the previously created Parts Unlimited
Project.

2. In the Parts Unlimited Project go to the Repos hub.

25
3. In the upper right corner of the PartsUnlimited pane, click Clone.

4. On the Clone Repository panel, with the HTTPS Command line option selected, click the
Copy to clipboard button next to the repo clone URL.

Note: You can plug this URL into any Git-compatible tool to get a copy of the codebase.

5. Close the Clone Repository panel.

26
6. Open Visual Studio Code(VS Code) and press Ctrl+Shift+P to show the Command
Palette.
Note: The Command Palette provides an easy and convenient way to access a wide variety
of tasks, including those provided by 3rd party extensions.

7. Type Git in the Command Palette and press enter to execute the Git: Clone command.

8. In the Provide repository URL or pick a repository source text box, paste the repo clone
URL you copied earlier in this task and press the Enter key.

27
9. Within the Select Folder dialog box, navigate to the C: drive, create a new folder named AZ-
400, select it, and then click Select Repository Location.

28
10. When prompted, log in to your Azure DevOps Account and wait for your repository to be
copied completely.

11. Once the cloning has completed, click Open to open the cloned repository.

Note: You can ignore any warnings raised about opening the projects. The solution may not be
in a buildable state, but that’s okay since we’re going to focus on working with Git and building
the project itself is not necessary.

29
8 SAVE WORK WITH COMMITS
In this section, you will step through several scenarios that involve the use of Visual Studio
Code to stage and commit changes.

When you make changes to your files, Git will record the changes in the local repository. You
can select the changes that you want to commit by staging the changes.

Commits are always made against your local Git repository, so you don’t have to worry about
the commit being perfect or ready to share with others. You can make more commits as you
continue to work and push the changes to others when they are ready to be shared.

8.1 Commit changes


1. In the Visual Studio Code window, at the top of the vertical toolbar, select the EXPLORER
tab, navigate to the

/PartsUnlimited-aspnet45/src/PartsUnlimitedWebsite/Models/CartItem.cs file and select


it. This will automatically display its content in the details pane.

30
2. Add a comment right above the [key] entry to the CartItem.cs file.
3. Press Ctrl+S to save the file.
Note: It doesn’t really matter what the comment is since the goal is just to make a change.

4. Select the Source Control tab to verify that Git recognized the latest change to the file
residing in the local clone of the Git repository.

31
5. Enter a commit message of “First Change” and press Ctrl+Enter to commit it locally.

6. If asked whether you would like to automatically stage your changes and commit them
directly, click Always.

32
7. In the lower left corner of the Visual Studio Code window, to the right of the master label, note
the Synchronize Changes icon of a circle with two vertical arrows pointing in the opposite
directions and the number 1 next to the arrow pointing up

8. Click the icon and, if prompted, whether to proceed, click OK to push and pull commits to and
from origin/master.

33
8.2 Review commits
In this task, you will use the Azure DevOps portal to review commits.
1. Switch to the Azure DevOps browser tab.

2. You can review the latest commits on Azure DevOps in the vertical navigational pane of the of
the Azure DevOps portal, in the Repos section, select Commits.

3. You can see the recent commit right at the top of the list.
Note: You’ll see many commit changes because we are using a public template provided by
Azure.

34
8.3 Stage changes
In this task, you will explore the use of staging changes by using Visual Studio Code.

Staging changes allows you to selectively add certain files to a commit while passing over the
changes made in other files.

1. Return to your Visual Studio Code.


2. Now open and Update the CartItem.cs class by changing the first comment to //My second
change and saving the file using Ctrl+S.

3. Open Category.cs as well.

35
4. Add a new comment, right above the public int CategoryId { get; set; } entry to
Category.cs file so there will be two files with changes. Save the file.

5. In the Visual Studio Code window, switch to the SOURCE CONTROL tab, hover the
mouse pointer over the CartItem.cs entry, and click the plus sign on the right side of that
entry.

36
Note: This stages the change to the CartItem.cs file only, preparing it for commit without
Category.cs.

6. With the SOURCE CONTROL tab selected, at the top of the pane, in the textbox, type Added
comments as the commit message and then click the ellipsis symbol, in the drop-down
menu, select Commit and, in the cascading menu, select Commit Staged.

37
7. In the lower left corner of the Visual Studio Code window, click the Synchronize Changes
button to synchronize the committed changes with the server and, if prompted, whether to
proceed, click OK to push and pull commits to and from origin/master

Note: Since only the staged changes were committed, the other changes are still pending
locally to be synchronized.

38
9 REVIEW HISTORY OF COMMITS
In this section, you will use the Azure DevOps portal to review history of commits.

Git’s use of the Branches and Merges feature works through pull requests, so the commit
history of your development doesn’t necessarily form a straight, chronological line. When you
use history to compare versions, think in terms of file changes between two commits instead
of file changes between two points in time. A recent change to a file in the master branch may
have come from a commit created two weeks ago in a feature branch but was only merged
yesterday.

9.1 Compare Files


In this task, you will step through commit history by using the Azure DevOps portal.

1. In the Source Control tab, select Category.cs, representing the non-staged version of the
file.

39
2. A comparison view is opened to enable you to easily locate the changes you’ve made. In our
case, it’s just one comment.

3. Go to the Commits view in your Azure DevOps portal to locate some of the source branches
and merges.

These provide a convenient way to visualize when and how changes were made to the
source.

40
4. Scroll down to the Merged PR 27 entry and hover the mouse pointer over it to reveal the
ellipsis symbol on the right side and then select Browse Files.

5. This view offers the ability to navigate around the state of the source at that commit so you
can review and download those files.

Note: Here, we will go and check the Cartlem.cs commit source

41
10 WORKING WITH BRANCHES
In this section, you will step through scenarios that involve branch management by using
Visual Studio Code and the Azure DevOps portal.

Committing changes to a branch will not affect other branches and you can share branches
with others without having to merge the changes into the main project. You can also create
new branches to isolate changes for a feature or a bug fix from your master branch and other
work. Since the branches are lightweight, switching between branches is quick and easy. Git
does not create multiple copies of your source when working with branches, but rather uses
the history information stored in commits to recreate the files on a branch when you start
working on it.

10.1 Create a new branch in your local repository


In this task, you will create a branch by using Visual Studio Code.
1. Return to Visual Studio Code.
2. In the lower left corner of the Visual Studio Code window,click the master branch.

42
3. In the pop-up window, select Create new branch from….option from the drop down

4. Enter the name “dev” for the new branch and press Enter.

5. Select the master as the reference branch.

Note: At this point, you are automatically switched to the dev branch.

43
6. You are now working on the newly created dev branch. Check that in the bottom left, you’ll
see dev now instead of master.

44
10.2 Working with branches
In this task, you will use the Visual Studio Code to work with a branch created in the previous

task.

Git keeps track of which branch you are working on and makes sure that, when you check out

a branch, your files match the most recent commit on that branch. Branches let you work with

multiple versions of the source code in the same local Git repository at the same time. You

can use Visual Studio Code to publish, check out and delete branches.

1. In the Visual Studio Code window, with the SOURCE CONTROL tab selected, in the lower
left corner of the Visual Studio Code window, click the Publish changes icon (directly to the
right of the dev label representing your newly created branch).

45
2. From the Azure DevOps browser tab, select Branches.

3. On the Mine tab of the Branches pane, verify that the list of branches includes dev.

4. Hover the mouse pointer over the dev branch entry to reveal the ellipsis symbol on the right
side.

46
5. Click the ellipsis, in the pop-up menu, select Delete branch, and, when prompted for
confirmation, click Delete.

6. Return to Visual Studio Code.


7. Click the dev branch.

47
8. Now here you’ll see that there are two dev branches listed.

The local (dev) branch is there because it’s not deleted when the server branch is deleted.
The server (origin/dev) is there because it hasn’t been pruned.

9. Select the master branch to check it out.

10. Press Ctrl+Shift+P to open the Command Palette.

11. Start typing “Git: Delete” and select Git: Delete Branch when it becomes visible.

12. Select the dev entry in the list of branches to delete.

48
13. In the lower left corner of the Visual Studio Code window, click the master entry again.

14. Now you can notice that the local dev branch is gone, but the remote origin/dev is still
showing.

15. Press Ctrl+Shift+P to open the Command Palette.

49
16. Start typing “Git: Fetch” and select Git: Fetch (Prune) when it becomes visible.

Note: This command will update the origin branches in the local snapshot and delete those
that are no longer there.

17. You can check in on exactly what these tasks are doing by selecting the Output window at
the bottom of the screen.

18. Now if you don’t see the Git logs in the output console, you may need to select Git as the
source.

19. Click the master branch.

50
20. Verify that the origin/dev branch no longer appears in the list of branches.

51
11 SUMMARY
In this lab, you used Visual Studio Code to clone an existing repository, save work with
commits, review history of changes, and work with branches.

52

You might also like