ce947d47e4 | ||
---|---|---|
.. | ||
images | ||
DEV02 - Getting Started with Git in Visual Studio.docx | ||
readme.md |
readme.md
Getting Started with Git in Visual Studio
Overview
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. In this Quick Start you will create a Git repository in Visual Studio Team Services, add a project to it, and manage changes via branches and pull requests.
Objectives
- Create a Visual Studio Team Services project with a Git repository.
- Commit a new project to the Git repository.
- Create a new branch and commit changes to it.
- Merge the new branch into the master branch via pull request.
Prerequisites
- Windows 10
- Visual Studio 2017
- Visual Studio Team Services subscription (Sign up for a free account here. Needs a Microsoft account)
Intended Audience
This Quick Start Challenge is intended for developers who are familiar with C# and Visual Studio. It does not require prior experience with Git.
Task 1: Create a new Visual Studio Team Services project
We'll start off by creating a new Visual Studio Team Services project backed by a Git repository.
- Log in to your Visual Studio Team Services account at https://[youraccount].visualstudio.com.
- Click New Project.
- Enter "Build Quick Start" as the Project name and ensure Git is selected as the Version control. Click Create.
- Once the project has been created, select the Code tab. It will be empty by default.
Task 2: Connect Visual Studio to your team project and Git repo
- Launch a new instance of Visual Studio 2017.
- In Team Explorer , click the Connect option to connect to your Visual Studio Team Services account.
- From the dropdown, select Add an account and follow the prompts to log in with the Microsoft account associated with your VSTS account.
- Select the Build Quick Start project created in the portal earlier and click Connect.
- Although there's no source in the project yet, you'll need to Clone the existing repo to get started. Click Clone.
- Click Settings to configure your Git settings.
- Click Global Settings.
- Enter a User Name and Email Address. You should use the email address associated with your VSTS login. Click Update.
Task 3: Add a new project to your Git repo
- Click the Home shortcut and click New under Solutions.
- Select the Visual C# | .NET Core category and the Console App (.NET Core) template. Git source control works with any kind of project, but we're going to keep it very simple here.
- Leave the default project name.
- Ensure Create new Git repository is checked. This will create your new project within the context of a local Git repo. Since Git is distributed in nature, you'll always work against this local repo and then periodically synchronize it with the server (or "origin").
- Click OK to create the project.
- In Team Explorer , click Changes.
- Enter "Initial commit" as the message and click Commit All.
8.Refresh the Visual Studio Team Services browser window. Note that it still doesn't have any code. This is because we committed the project locally and have not yet pushed those changes to the server.
- Back in Team Explorer , note the local commit message. In order to share the committed changes to the server, click Sync to navigate to the Synchronization view.
- Because there is no remote branch yet, the only option available is to push the local branch to the server. Click Push to push the branch.
- Refresh the VSTS browser window to notice that there is now code committed to the server.
Task 4: Manage a change via branch and pull request
- From the views dropdown, select Branches.
- Right-click the master branch and select New Local Branch From. This will create a new branch from the master branch so that you can work against it and even perform server commits that do not impact work being done on the master branch itself.
- Enter a Name of "my_branch" and click Create Branch. For complex projects, it is recommended that you follow a slash-delimited branch naming convention, such as "username/feature".
- Double-click my_branch to select it as the active branch.
- In Solution Explorer , note that the source file now has a blue padlock glyph next to it. This indicates that the file is under source control.
- Open Program.cs and update the main line of code to the following (adding "Git").
Console.WriteLine("Hello Git World!");
- Note now that Solution Explorer shows a red check to indicate that this file has been modified from the version in source control.
- In Team Explorer , select Changes from the views dropdown. This will provide a list of files that have been changed from source control.
- Right-click Program.cs and select Compare with Unmodified.
- In the diff view, you can see exactly what lines (and sections of lines) have been changed.
- In Team Explorer , enter a message of "Text update" and select Commit All | Commit All and Push. This will commit the changes to the current branch and push the branch to the origin.
- In the VSTS browser window, select the Branches tab.
- Hover over the my_branch row and click the New pull request link that appears. A pull request communicates your desire to have the target branch merged into another branch.
- You can greatly customize who is included in the pull request and what information they might need to make a decision, such as linked work items. In this case, click New pull request to create using default settings.
- Once created, reviewers can review the code and have discussions about the pull request. In this case we'll skip to the end and assume it's a good set of changes. Click Complete.
- Note that the option to delete the source branch is selected by default when merging. This makes it easier to keep the repo relatively clean. Click Complete merge.
- Select the Branches tab and note that there is now only the master branch.
- Click the updated link (probably "updated just now" ).
- Now you can also view the history of this branch and even drill into specific pull requests and commits.
Summary
Congratulations on completing this Quick Start Challenge! In this lab, you learned how to use Visual Studio with Git.
Additional Resources
If you are interested in learning more about this topic, you can refer to the following resources:
Documentation : https://www.visualstudio.com/learn-git/