Essentials On Azure DevOps Services and GitHub Book 3
Essentials On Azure DevOps Services and GitHub Book 3
and GitHub
Conditions and Terms of Use
Microsoft Confidential
http://www.microsoft.com/en-us/legal/intellectualproperty/Permissions/default.aspx
How to View This Presentation
• Switch to the Notes Page view:
o Click View on the ribbon, and select Notes Page
o Use the Page Up or Page Down keys to navigate
o Zoom in or out as needed
Microsoft Confidential
Introduction and Logistics
• Your trainer
• You:
o Your role
o Your company
o Your experience in this technology area
o Your goals for this workshop
Microsoft Confidential
Module 3: Azure Repos - Git
Module Overview
Microsoft Confidential
Overview
• Git Overview
• Develop by Using Git
• Collaborate by Using Git
• Use Branches
Microsoft Confidential
Module 3: Azure Repos - Git
Microsoft Confidential
Overview
• Version Control Systems
• Git
• Benefits of Git
• Git Basics
• Install and Set Up Git
Microsoft Confidential
Microsoft Confidential
Version Control Systems
Microsoft Confidential 9
Git
• Git is the most commonly used version control system today and is quickly becoming the standard for
version control
• Git is a distributed version control system, meaning your local copy of code is a complete version
control repository. These fully-functional local repositories make it is easy to work offline or remotely
• You commit your work locally, and then sync your copy of the repository with the copy on the server
• This paradigm differs from centralized version control where clients must synchronize code with a server
before creating new versions of code
• Nearly every development environment has Git support and Git command line tools run on every major
operating system.
Microsoft Confidential 10
Benefits of Git
• Simultaneous development
• Faster releases
• Built-in integration
• Strong community support
• Pull Requests
• Branch Policies
Microsoft Confidential 11
Git Basics - Commit
• Every time you save your work, Git creates a commit
• A commit is a snapshot of all your files at a point in time
• If a file has not changed from one commit to the next, Git uses the previously stored file. This
design differs from other systems which store an initial version of a file and keep a record of
deltas over time
• Commits create links to other commits, forming a graph of your development history
• You can revert your code to a previous commit, inspect how files changed from one commit to
the next, and review information such as where and when changes were made
• Commits are identified in Git by a unique cryptographic hash of the contents of the commit.
Because everything is hashed, it is impossible to make changes, lose information, or corrupt
files without Git detecting it
Microsoft Confidential 12
Git Basics - Branches
• Git provides tools for isolating changes and later merging them back together
• Branches, which are lightweight pointers to work in progress, manage this separation
• Once your work created in a branch is finished, merge it back into your team’s main (or master) branch
• Customizable default branch name for Git
Microsoft Confidential 13
Git Basics – Files and Commits
• Files in Git are in one of three states: modified, staged,
or committed
• When you first modify a file, the changes exist only in
your working directory. They are not yet part of a commit
or your development history
• You must stage the changed files you want to include in
your commit. The staging area contains all changes that
you will include in your next commit
• Once you’re happy with the staged files, commit them
with a message describing what changed. This commit
becomes a part of your development history
• Staging lets you pick which file changes to save in a
commit so you can break down large changes into a
series of smaller commits. When you reduce the scope of
your commits, it’s easier to review the commit history to
find specific file changes
Microsoft Confidential 14
Git Basics - History
• Centralized systems store a separate history for each file in a repository.
• Git stores history as a graph of snapshots of the entire repository. These snapshots—which are
called commits in Git—can have multiple parents, creating a history that looks like a graph instead
of a straight line.
Microsoft Confidential 15
Git Basics - Repositories
• A Git repository, or repo, is a folder that
you've told Git to help you track file changes
in.
• You can have any number of repos on your
computer, each stored in their own folder.
• You can have any number of Git repos in a
Project
• Each Git repo is independent, so changes
saved in one Git repo don't affect the
contents of another.
• A Git repo contains every version of every
file saved in the repo.
• Most teams will use a central repo hosted on
a server everyone can access to coordinate
their changes.
Microsoft Confidential
Microsoft Confidential
Import Repositories from TFVC to Git
• You can migrate code
from an existing TFVC
repository to a new
Git repository within
the same
organization.
• You can migrate up
to 180 days of history
starting from the
most recent
changeset.
Microsoft Confidential 17
Install and Set Up Git
• Windows
o Download and install Git for Windows
o Once installed, you’ll be able to use Git from the command prompt or PowerShell
o Update Git for Windows by downloading a new version of the installer, which will update Git for Windows in place
and keep all of your settings.
• macOS
o macOS 10.9 (Mavericks) or higher will install Git the first time you try to run Git from the Terminal
o We recommend installing Git through Homebrew and using the Homebrew tools to keep Git up-to-date.
• Linux
o Use your Linux distribution’s package management system to install and update Git.
o E.g. on Ubuntu:
> sudo apt-get install git
• It’s important to keep Git up to date, just like all the other software on your machine. Updates protect you
from security vulnerabilities, fix bugs, and give you access to new features.
Microsoft Confidential 18
Demo 1: Git Overview
Microsoft Confidential
19 Microsoft Confidential
Lesson Knowledge Check
1. What is Git?
2. What are the main differences between TFVC and Git?
3. What is a Commit?
Microsoft Confidential
Lesson Summary
• In this lesson, you learned about:
o Version Control Systems
o Git
o Benefits of Git
o Git Basics
o Install and Set Up Git
Microsoft Confidential
Module 3: Azure Repos - Git
Microsoft Confidential
Overview
• Start from a Local Repository
• Start from the Remote Repository
• Save work with Commits
• Use .gitignore
• Browse
• CodeLens
• Git Command Reference
Microsoft Confidential
Microsoft Confidential
Start from a Local Repository
• Manage any folder with source code in Git by creating a repo for them. Later you can connect
this Git repo to a remote Git repo to share your work with others.
Microsoft Confidential
Microsoft Confidential
Start from the Remote Repository
• Create a complete local copy of an existing Git repo by cloning it.
• Cloning a repo downloads all commits and branches in the repo and sets up a named relationship
with the existing repo you cloned.
• Use this relationship to interact with the existing repo, pushing and pulling changes to share code
with your team.
• You can clone
o from Azure DevOps Services / Azure DevOps Server (formerly known as TFS) or another Git provider using
Team Explorer
o from command line using the “git clone” command
Microsoft Confidential
Microsoft Confidential
Save work with Commits
• Stage individual file changes. Only staged changes will be added to the next commit
• Enter a commit message describing your changes
• Commits are created in your local Git repository. Continue to create commits as you work,
pushing your changes to the team when they are ready to share.
Microsoft Confidential
Microsoft Confidential
Use .gitignore
• Avoid file clutter in your work and in your repository
o Example: Locally compiled binary files
Microsoft Confidential
Browse
• Browse your local Git repository from File
Explorer or Git command prompt
• Browse remote repositories from the web portal
o View, download and compare files
o View commit details
o View and create branches
o View tags
o View and complete pull requests
o Edit files, etc
Microsoft Confidential
CodeLens
Microsoft Confidential
Git Command Reference
How do I? Git Command Line
Create a repo in a new folder git init foldername
Clone a repo into a local folder git clone URL
foldername
Add a remote git remote add name
url
Microsoft Confidential
Demo 2: Develop By Using
Git
Microsoft Confidential
32
Lesson Knowledge Check
1. What is the purpose of .gitignore?
2. Name two Git operations that can be performed from the command prompt
3. How would you open the Git command prompt?
4. True / False : You can browse files in a Git repository using Source Control Explorer
Microsoft Confidential
Lesson Summary
• In this lesson, you learned about:
o Start from a Local Repository
o Start from the Remote Repository
o Save work with Commits
o Use .gitignore
o Browse
o CodeLens
o Git Command Reference
Microsoft Confidential
Module 3: Azure Repos - Git
Microsoft Confidential
Overview
• Fetch
• Pull
• Push
• Undo / Reset / Revert
• Forks
• Tags
• Notifications
Microsoft Confidential
Microsoft Confidential
Fetch
• You download changes to your local branch from the remote through fetch. Fetch asks the remote repo for
all commits and new branches that others have pushed but you don't have and downloads them into your
repo, creating local branches as needed.
• Fetch does not merge any changes into your local branches, it only downloads the new commits for your
review.
• To help keep your branches list clean and up to date, configure Git to prune remote branches during fetch.
• When to fetch
o When you want to preview the changes from your team before you integrate them into your work
o Before you get a copy of a branch that someone on your team has published
• Merge takes the commits retrieved from fetch and tries to add them to your local branch.
Microsoft Confidential
Microsoft Confidential
Pull
• Pull does a fetch and then a merge to download the commits and update your local branch in one
command instead of two.
• When to pull
o Use pull to quickly bring your branch up to date with the remote when you aren't worried about reviewing the
changes before merging them into your own branch.
• Pulling updates files in your open project, so make sure to commit your changes before pulling.
Microsoft Confidential
Microsoft Confidential
Push
• Share changes made in commits and branches using the push command
• Push your branches to the remote repository, where Git takes the commits and adds them to an existing
branch on the remote or creates a new branch with the same commits as your local branch.
• When to push
o Use push when you are ready to commit changes to the team’s remote Git repository
Microsoft Confidential
Microsoft Confidential
Undo / Reset / Revert
• When undoing changes in Git, first decide what type of changes you are looking to undo. These changes
fall into three categories:
o Discard uncommitted changes to a file, bringing the file back to the version in the last commit (Undo Changes)
o Reset your local branch to a previous commit. Use reset to bring a branch in your local repository back to the
contents of a previous commit. The most common use of the reset command is to simply discard all changed files
since the last commit and return the files to the state they were in at the most recent commit.
o Revert changes pushed to a remote branch and shared with others. The revert command creates a new commit
that undoes the changes on a previous commit. No history is rewritten in a revert, making it safe to use when
working with others.
Microsoft Confidential 41
Forks
• A fork is a complete copy of a repository, including all files, commits, and (optionally) branches.
• The new fork acts as if someone cloned the original repository, then pushed to a new, empty repository.
• After a fork has been created, new files, folders, and branches are not shared between the repositories
unless a pull request carries them along
• Once you're ready to share those changes, it's easy to use pull requests to push the changes back to the
original repository.
• When to fork
o You can create a fork to suggest changes to a project when you don't have permissions to write to the original
project directly.
Microsoft Confidential 42
Tags
• Lightweight tags are a pointer to specific commit
• Annotated tags contain more information such as the tagger, message, and date
• You can create annotated tags using the web portal, and starting with Visual Studio 2017 Update 6, you
can create both lightweight and annotated tags from within Visual Studio
Microsoft Confidential 43
Notifications
Microsoft Confidential 44
Demo 3: Collaborate By
Using Git
Microsoft Confidential
Lesson Knowledge Check
1. When do you perform a push operation?
2. When do you perform a reset operation?
Microsoft Confidential
Lesson Summary
• In this lesson, you learned about:
o Fetch
o Pull
o Push
o Undo / Reset / Revert
o Forks
o Tags
o Notifications
Microsoft Confidential
Module 3: Azure Repos - Git
Microsoft Confidential
Overview
• Git Branches
• Git Branching Guidance
• Create a Branch
• Conflict Resolution
• Rebase / Merge
• Cherry-pick
• Pull Requests
• Branch Policies
Microsoft Confidential
Git Branches
• Git branches aren't much more than a small reference that keeps an exact history of commits, so they are
very cheap to create
• 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
• 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—it uses the history
information stored in commits to recreate the files on a branch when you start working on it
Microsoft Confidential 51
Git Branches (continued)
• Use branches to switch contexts, suspend work, and isolate risks
• In Git,
o The value of branching is higher
o The complexity and cost of branching are lower
o Development teams are encouraged to branch (often)
• Some people create a “topic” branch for each task they perform
o When satisfied with the work, they merge it back into the master branch
o You have the option to publish the branch into a remote repository (such as a Git team project) to collaborate with
others
• Customizable default branch name for Git
• Default branch name for the org-level so that projects default to the organization setting when the branch
name isn't specified.
Microsoft Confidential
Microsoft Confidential
Git Branching Guidance
• Keep your branch strategy simple
o Use feature branches for all new features and bug fixes
▪ Name your feature branches by convention
▪ Use feature flags to manage long-running branches
o Merge feature branches into the master branch using pull requests
o Keep a high quality, up-to-date master branch by using branch policies
• Manage releases
o Use release branches
o Port changes back to the master branch
Microsoft Confidential
Microsoft Confidential
How are Git Branches Created?
• Branch creates a reference in Git for the new branch and a pointer back to the parent commit so Git can
keep a history of changes as you add commits to the branch.
• When you are working with a branch that someone else shared, Git keeps an upstream tracking
relationship to associate the branch on the local repo with the corresponding branch on the remote repo.
• Git always adds new commits to the current local branch. Check what branch you are working on before
you commit so that you don't commit changes to the wrong branch. Swap between local branches using
the checkout command.
Microsoft Confidential
Microsoft Confidential
Create a Branch
• Create a branch from Visual Studio, Visual Studio Code
• Use the Web Portal
• Collaborate or preserve the work you have done in a branch by publishing it
• Merge the work you have done in one branch into another branch
Microsoft Confidential
Microsoft Confidential
Conflict Resolution
• When you merge one branch into another, file
changes from commits in one branch can conflict
with the changes in the other.
• Git attempts to resolve these changes by using the
history in your repo to determine what the merged
files should look like.
• When it isn't clear how to merge changes, Git halts
the merge and tells you which files conflict.
• The most common merge conflict situation is when
you pull updates from a remote branch to your local
branch. Resolve these conflicts in the same way -
create a merge commit on your local branch
reconciling the changes and complete the merge.
Microsoft Confidential 56
Rebase / Merge
• One of the tradeoffs from the Git feature branch workflow is that you do not actively manage your version
control history. Git creates this history as you save your code in your commits and merges changes back
into the master branch with pull requests.
• This generated history can get complicated when you need to update a feature branch with changes from
the main branch to catch up on work committed by others. Your commit history will diverge from the
master branch at multiple points, making it hard to follow.
• When working in a branch, you may want to incorporate the latest changes from the master branch into
your branch. There are two different approaches you can use to perform this: rebase or merge:
o Rebase takes the changes made in the commits in your current branch and replays them on the history of another
branch. The commit history of your current branch will be rewritten so that it starts from the most recent commit in
the target branch of the rebase.
o Merge merges the changes from the source branch to the target branch using a merge commit, which becomes
part of the commit history.
• A suggested approach is to allow rebasing local changes that you have made but haven't shared with
others, but to merge once you are sharing changes with others.
Microsoft Confidential 57
Merge
Before After
Rebase
Before After
Microsoft Confidential 58
Cherry-pick
• Copy commits from one branch to another using cherry-pick. Unlike a merge or rebase, cherry-pick only
brings the changes from the commits you select, instead of all the changes in a branch.
• Cherry-pick is a great way to tackle these common problems:
o Accidentally committing on the wrong branch. Cherry-pick the change(s) over to the correct branch
and then reset the original branch to the previous commit.
o Pulling out a set of commits made in a feature branch so you merge them back to your master
branch sooner.
o Porting in specific commits from the master branch without rebasing your branch.
Microsoft Confidential 59
Pull Requests
• Pull requests combine the review and merge of your code into a single collaborative process. Once you're
done fixing a bug or new feature in a branch, create a new pull request. Add the members of the team to
the pull request so they can review and vote on your changes.
• Use pull requests to review work in progress and get early feedback on changes.
• There's no commitment to merge the changes as the owner can abandon the pull request at any time.
Microsoft Confidential
Microsoft Confidential
Pull Request Workflow
• Create a pull request in the
o Repos view on the web from
▪ the Pull requests tab, or
▪ the Files tab
• Add any team member who you would like to review the changes
• Pull request reviewers will see the proposed updates to the branch in the form of file differences between
the two branches. Reviewers can add comments on any of the changes.
• If the changes need much more development to complete, you can abandon the pull request. You can
later open up a new pull request to revisit the changes and link to the conversations that took place in the
abandoned pull request.
• Complete your pull request after the reviewers approve of the changes.
• You must resolve any merge conflicts between the pull request branch and the target branch. Git adds a
new commit (the merge commit) to the end of the master branch. This merge commit links the earlier
history of both the master branch and the commits for the branch that was merged as part of the pull
request.
• Add attachments while creating a pull request
Microsoft Confidential 61
Branch Policies
• Branch policies are an important part of the Git workflow and enable you to:
o Isolate work in progress from the completed work in your master branch
o Guarantee changes build before they get to master
o Limit who can contribute to specific branches
o Automatically include the right reviewers for every code change
o Enforce best practices with required code reviewers
Microsoft Confidential
Microsoft Confidential
Configure Branch Policies
• Available branch policy configurations:
o Require a minimum number of reviewers
o Check for linked work items
o Check for comment resolution
o Limit merge types
o Build validation
o Require approval from additional services
o Automatically include code reviewers
Microsoft Confidential
Microsoft Confidential
Demo 4: Use Branches
Microsoft Confidential
64
Lesson Knowledge Check
1. What is a branch? How do you use them?
2. Can you create a branch from a specific commit or tag?
3. What is a pull request?
Microsoft Confidential
Microsoft Confidential
Lesson Summary
• In this lesson, you learned about:
o Git Branches
o Git Branching Guidance
o Create a Branch
o Publish a Branch
o Merge a Branch
o Conflict Resolution
o Rebase / Merge
o Cherry-pick
o Pull Requests
o Branch Policies
Microsoft Confidential
Microsoft Confidential
Module Summary
• In this module, you learned about:
o Git Overview
o Develop by using Git
o Collaborate by using Git
o Use branches
Microsoft Confidential
Lab: Azure Repos - Git
Microsoft Confidential
Microsoft Confidential