Version Controlling With Git in Visual Studio Code and Azure DevOps
Version Controlling With Git in Visual Studio Code and Azure DevOps
Overview
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.
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 with Azure DevOps.
Prerequisites
1. Open Visual Studio Code. In this task, you 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.
2. From the main menu, select Terminal | New Terminal to open a terminal window.
3. Execute the command below to configure a credential helper.
4. git config --global credential.helper wincred
5. The commands below will configure your user name and email for Git commits. Replace
the parameters with your preferred user name and email and execute them.
6. git config --global user.name "John Doe"
7. git config --global user.email [email protected]
3. Click Clone.
4. Click the Copy to clipboard button next to the repo clone URL. You can plug this URL
into any Git-compatible tool to get a copy of the codebase.
What’s in a commit?
The file(s) changed in the commit. Git keeps the contents of all file changes in your repo
in the commits. This keeps it fast and allows intelligent merging.
A reference to the parent commit(s). Git manages your code history using these
references.
A message describing a commit. You give this message to Git when you create the
commit. It’s a good idea to keep this message descriptive, but to the point.
1. From the Explorer tab,
open /PartsUnlimited-aspnet45/src/PartsUnlimitedWebsite/Models/CartItem.cs.
2. Add a comment to the file. It doesn’t really matter what the comment is since the goal is
just to make a change. Press Ctrl+S to save the file.
1. Switch to the Azure DevOps browser tab. You can review the latest commits on Azure
DevOps under the Commits tab of the Repos hub.
Staging changes allows you to selectively add certain files to a commit while passing over the
changes made in other files.
3. Open Category.cs as well.
4. Add a new comment to Category.cs so there will be two files with changes. Save the file.
5. From the Source Control tab, click the Stage Changes button for CartItem.cs.
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.
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.
Your Git workflow should create and use branches for managing features and bugfixes. The rest
of the Git workflow, such as sharing code and reviewing code with pull requests, all work through
branches. Isolating work in branches makes it very simple to change what you are working on by
simply changing your current branch.
Git keeps track of which branch you are working on and makes sure that when you checkout a
branch your files match the most recent commit on the 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.
6. Note 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. Select the master branch to check it out.
7. Press Ctrl+Shift+P to open the Command Palette.
8. Start typing “Git: Delete” and select Git: Delete Branch when it becomes visible.
11. Note that the local dev branch is gone, but the remote origin/dev is still showing.
15. Note that if you don’t see the Git logs in the output console, you may need to
select Git as the source.
3. Enter a name of “release” for the new branch. Use the Work items to link dropdown to
select one or more work items to link to this new branch. Click Create branch to create it.
4. After the branch has been created, it will be available in the list.
8. Click the master branch.
1. Return to Azure DevOps and click the Delete branch from the more actions drop down
to delete it.
2. You can Restore branch if you want by searching for an exact branch name.
Select Restore branch as shown below.
Task 3: Locking a branch
Locking is ideal for preventing new changes that might conflict with an important merge or to
place a branch into a read-only state. Alternatively, you can use branch policies and pull requests
instead of locking if you just want to ensure that changes in a branch are reviewed before they
are merged.
Locking does not prevent cloning of a repo or fetching updates made in the branch into your
local repo. If you lock a branch, share with your team the reason why and make sure they know
what to do to work with the branch after it is unlocked.
1. While it may not seem like much, the product team has decided that this version of the
site is exactly what’s needed for v1.1. In order to mark it as such, navigate to the Tags tab.
2. Click Create Tag.
3. Enter a name of “v1.1” and a Description of “Great release!”. Click Create.
4. You have now tagged the project at this release. You could tag commits for a variety of
reasons and Azure DevOps offers the flexibility to edit and delete them, as well as
manage their permissions.
##
##
3. That’s it. Your repo is ready. You can now clone it with Visual Studio or your tools of
choice.
1. Sometimes you’ll have a need to rename or delete a repo, which is just as easy.
Open Project settings.
2. Select Repositories under Repos.