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

Exercise 1: Configuring the lab environment

Task 1: Configuring Visual Studio Code

  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.

     git config --global credential.helper wincred
  4. 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.

     git config --global user.name "John Doe"
    git config --global user.email johndoe@example.com

Exercise 2: Cloning an existing repository

Task 1: Cloning an existing repository

  1. In a browser tab, navigate to your team project on Azure DevOps.

  2. Getting a local copy of a Git repo is called “cloning”. Every mainstream development tool supports this and will be able to connect to Azure Repos to pull down the latest source to work with. Navigate to the Repos hub.

  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.

  5. Open an instance of Visual Studio Code.

  6. Press Ctrl+Shift+P to show the Command Palette. The Command Palette provides an easy and convenient way to access a wide variety of tasks, including those provided by 3rd party extensions.

  7. Execute the Git: Clone command. It may help to type “Git” to bring it to the shortlist.

  8. Paste in the URL to your repo and press Enter.

  9. Select a local path to clone the repo to.

  10. When prompted, log in to your Azure DevOps account.

  11. Once the cloning has completed, click Open Repository. 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.

Task 2: Installing the Azure Repos extension for Visual Studio Code

  1. The Azure Repos extension provides convenient access to many features of Azure DevOps. From the Extensions tab, search for “Azure Repos” and click Install to install it.

  2. Click Reload once the extension has finished installing. If this option is not available, reopen Visual Studio Code.

  3. Press Ctrl+Shift+P to show the Command Palette.

  4. Search for “Team” to see all the new commands that are now available for working with Azure Repos. Select Team: Signin.

  5. Select Authenticate and get an access token automatically. Note that you could alternatively provide the token created earlier if following the manual path.

  6. Copy the provided token and press Enter to launch a browser tab.

  7. Paste the code in to the login box and click Continue.

  8. Select the Microsoft account associated with your Azure DevOps account.

  9. When the process has complete, close the browser tab.

Exercise 3: Saving work with commits

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.

What’s in a commit?

Git commits consists of the following:

  • 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.

Task 1: Committing changes

  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.

  3. Select the Source Control tab to see the one change to the solution.

  4. Enter a commit message of “My commit” and press Ctrl+Enter to commit it locally.

  5. If asked whether you would like to automatically stage your changes and commit them directly, click Always. We will discuss staging later in the lab.

  6. Click the Synchronize Changes button to synchronize your changes with the server. Confirm the sync if prompted.

Task 2: Reviewing commits

  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.

  2. The recent commit should be right at the top.

Task 3: Staging changes

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

  1. Return to Visual Studio Code.

  2. Update the open CartItem.cs class by editing the comment you made earlier and saving the file.

  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.

  6. This will prepare CartItem.cs for committing without Category.cs.

  7. Enter a comment of “Added comments”. From the More Actions dropdown, select Commit Staged.

  8. Click the Synchronize Changes button to synchronize the committed changes with the server. Note that since only the staged changes were committed, the other changes are still pending locally.

Exercise 4: Reviewing history

Git uses the parent reference information stored in each commit to manage a full history of your development. You can easily review this commit history to find out when file changes were made and determine differences between versions of your code using the terminal or from one of the many Visual Studio Code extensions available. You can also review changes using the Azure DevOps portal.

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.

Task 1: Comparing files

  1. In the Source Control tab, select Category.cs.

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

  3. Press Ctrl|Shift+P to open the Command Palette.

  4. Start typing “Team” and select Team: View History when it becomes available. This is a feature of the Azure Repos extension that makes it easy to jump right to the history of this file in a new browser tab.

  5. Note the history of Category.cs. Close the newly created tab, which will should return you to the Commits tab from earlier.

  6. Scroll down in the Commits view 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.

  7. From the dropdown for Merged PR 27, select Browse Files.

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

Exercise 5: Working with branches

You can manage the work in your Azure DevOps Git repo from the Branches view on the web. You can also customize the view to track the branches you care most about so you can stay on top of changes made by your team.

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.

Task 1: Creating a new branch in your local repository

  1. Return to Visual Studio Code.

  2. Click the master branch from the bottom left.

  3. Select Create new branch from….

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

  5. Select the master as the reference branch.

  6. You are now working on that branch.

Task 2: Working with branches

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.

  1. Click the Publish changes button next to the branch.

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

  3. You should see the newly pushed dev branch. Click the Delete branch button to delete it. Confirm the delete.

  4. Return to Visual Studio Code.

  5. Click the dev branch.

  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.

  9. There is only one local branch to delete, so select it.

  10. Click the master branch.

  11. Note that the local dev branch is gone, but the remote origin/dev is still showing.

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

  13. Start typing “Git: Fetch” and select Git: Fetch (Prune) when it becomes visible. This command will update the origin branches in the local snapshot and delete those that are no longer there.

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

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

  16. Click the master branch.

  17. The origin/dev branch should no longer be in the list.

Exercise 6: Managing branches from Azure DevOps

In addition to all the functionality available in Visual Studio Code, you can also manage your repo branches from the Azure DevOps portal.

Task 1: Creating a new branch

  1. Switch to the Azure DevOps browser tab.

  2. Navigate to Repos | Branches. Click New branch.

  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.

  5. Return to Visual Studio Code.

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

  7. Start typing “Git: Fetch” and select Git: Fetch when it becomes visible. This command will update the origin branches in the local snapshot.

  8. Click the master branch.

  9. Select origin/release. This will create a new local branch called “release” and check it out.

Task 2: Deleting a branch

  1. Return to Azure DevOps and click the Delete button that appears when you hover over the release branch to delete it.

  2. However, maybe we should keep it around for a little longer. From its context menu, select Restore branch.

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. From the master context menu, select Lock.

  2. The branch is now locked.

  3. Now Unlock the branch using the same process.

Task 4: Tagging a release

  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.

Exercise 7: Managing repositories

You can create Git repos in team projects to manage your project’s source code. Each Git repo has its own set of permissions and branches to isolate itself from other work in your project.

Task 1: Creating a new repo from Azure DevOps

  1. From the project Add dropdown, select New repository.

  2. Set the Repository name to “New Repo”. Note that you also have the option to create a file named README.md. This would be the default markdown file that is rendered when someone navigates to the repo root in a browser. Additionally, you can preconfigure the repo with a .gitignore file. This file specifies which files, based on naming pattern and/or path, to ignore from source control. There are multiple templates available that include the common patterns and paths to ignore based on the project type you are creating. Click Create.

  3. That’s it. Your repo is ready. You can now clone it with Visual Studio or your tools of choice.

Task 2: Deleting and renaming Git repos

  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.

  3. From the New Repo context menu, select Delete repository. Alternatively, you could rename it here.

  4. Enter the name “New Repo” to confirm the repo and click Delete.

来源:https://azuredevopslabs.com/labs/azuredevops/git/

Version Controlling with Git in Visual Studio Code and Azure DevOps的更多相关文章

  1. Visual Studio Code初探

    作者:Grey 本文的GIF动画均使用ScreenToGif进行录制. 摘要 微软今年发布了一款运行于 OS X,Windows 和 Linux 之上的免费跨平台编辑器: Visual Studio ...

  2. Visual Studio Code 使用Git进行版本控制

    Visual Studio Code 使用Git进行版本控制 本来认为此类教程,肯定是满网飞了.今天首次使用VS Code的Git功能,翻遍了 所有中文教程,竟没有一个靠谱的.遂动笔写一篇. 请确保你 ...

  3. Visual Studio Code 显示隐藏的.git文件和目录

    在默认设置中,Visual Studio Code 将下列文件文件排除在显示列表中: "files.exclude": { "**/.git": true, & ...

  4. 如何在"Visual Studio Code"中使用" Git" 进行版本控制

    如何在"Visual Studio Code"中使用" Git" 进行版本控制 本来认为此类教程,肯定是满网飞了.今天首次使用VS Code的Git功能,翻遍了 ...

  5. Visual Studio Code 配置指南

    Visual Studio Code (简称 VS Code)是由微软研发的一款免费.开源的跨平台文本(代码)编辑器.在我看来它是「一款完美的编辑器」. 本文是有关 VS Code 的特性介绍与配置指 ...

  6. 打造TypeScript的Visual Studio Code开发环境

    打造TypeScript的Visual Studio Code开发环境 本文转自:https://zhuanlan.zhihu.com/p/21611724 作者: 2gua TypeScript是由 ...

  7. 剖析并利用Visual Studio Code在Mac上编译、调试c#程序

    0x00 前言 一周多以前的微软的Build大会上,微软发布了一个让很多人眼前一亮的工具,也是本文的主角——Visual Studio Code.很多使用Windows的朋友都很高兴,认为又多了一个很 ...

  8. ASP.NET Core 中文文档 第二章 指南(1)用 Visual Studio Code 在 macOS 上创建首个 ASP.NET Core 应用程序

    原文:Your First ASP.NET Core Application on a Mac Using Visual Studio Code 作者:Daniel Roth.Steve Smith ...

  9. 剖析并利用Visual Studio Code在Mac上编译、调试c#程序【转】

    0x00 前言 一周多以前的微软的Build大会上,微软发布了一个让很多人眼前一亮的工具,也是本文的主角——Visual Studio Code.很多使用Windows的朋友都很高兴,认为又多了一个很 ...

随机推荐

  1. Vuex-全局状态管理【传递参数】

    src根目录 新建store文件夹,新建index.js 作为入口 在store文件夹中 新建modules文件夹 modules文件夹中,新建 a.js b.js 2个文件 a.js const s ...

  2. 第七周作业—N42-虚怀若谷

    一.简述OSI七层模型和TCP/IP五层模型 1. OSI七层模型 物理层:二进制传输,为启动.维护以及关闭物理链路定义了电气规范.机械规范.过程规范和功能规范:实际的最终信号的传输是通过物理层实现的 ...

  3. css3中的过渡效果和动画效果

    一.CSS3 过渡 (一).CSS3过渡简介 CSS3过渡是元素从一种样式逐渐改变为另一种的效果. 实现过渡效果的两个要件: 规定把效果添加到哪个 CSS 属性上 规定效果的时长 定义动画的规则 过渡 ...

  4. Navicat 出现的[Err] 1146 - Table 'performance_schema.session_status' doesn't exist已解决

    [Err] 1146 - Table 'performance_schema.session_status' doesn't exist已解决   刚刚接触MySQL,就往数据库添加数据,就遇到这个问 ...

  5. 总结PHP缓存技术的多种方法

    这里所说的数据缓存是指数据库查询PHP缓存机制,每次访问页面的时候,都会先检测相应的缓存数据是否存在,如果不存在,就连接数据库,得到数据,并把查询结果序列化后保存到文件中,以后同样的查询结果就直接从缓 ...

  6. leetcode打卡

    leetcode刷题打卡 刷题链接 夸夸群 刷题记录链接 期中颜色不一样的,是刷题中遇到问题的,以后需要强化 [x] 6.1 打卡 [x] 6.2 打卡 中间因个人原因没做题,后面慢慢补上 [x] 6 ...

  7. SPOJ 2798 QTREE3 - Query on a tree again!

    原oj题面 Time limit 2000 ms Memory limit 1572864 kB Code length Limit 50000 B OS Linux Language limit A ...

  8. python大佬养成计划----HTML DOM

    什么是DOM? DOM (Document Object Model) 译为文档对象模型,是 HTML 和 XML 文档的编程接口.HTML DOM 定义了访问和操作 HTML 文档的标准方法.DOM ...

  9. vue引入静态js文件

    由于一些演示,需要对编码名称等可快速进行修改,需要页面方便配置.由于build后的vue项目基本已经看不出原样,因此需要创建一个文件,并在打包的时候不会进行编译. vue-cli 2.0的作法是在st ...

  10. inode节点用尽处理

    linux inode已满解决方法 原文 今天login server的一个网站,发现login后没有生成session.根据以往经验,一般是空间已满导致session文件生成失败. df -h Fi ...