I had written an article introducing Git feature of Visual Studio Team Services (VSTS). VSTS was called Visual Studio Online (VSO) at that time. In that article, we walked through a scenario of creating a new team project, cloning the remote repository, and adding a new solution to it.
VSTS Git Pull Request
In this article, we will look at a different scenario where a solution in a local repository needs to be added to a remote repository. We will also have an overview of a feature in VSTS Git called Pull Request which enables code review before the code is merged from a temporary branch to a master or release branch.
Adding Local Repository to Remote Repository
Many a times, we begin coding by creating a local solution which is not added to the source control. Sometimes, we may also download a project created by someone else to be used in our solution. In such situations, we need to first add the solution to the local repository, and then synchronize it with the remote repository on VSTS.
First create a solution and add a project under it. Note that the solution as well as the project are not added to the source control at the time of creation. We can do some code edits in the files. Let us now add the project to a local Git repository. To do so, right click on the solution and select add to source control.
It is also possible to create a new git repository and add the solution to that repository while creating the project.
Figure 1: Create new project and a new git repository
If we look at the team explorer now, it will show various git operations possible at this moment.
Figure 2: Git without connecting to VSTS
After the edits are done in the project, we will do a commit.
Figure 3: Commit code to local repository
After the commit, check the bottom right corner of the Visual Studio where we will be able to see the status of the local repository. It shows that we are working in the master branch of the local repository, and there are 3 commits that are not yet published. If we want to share this project with our team members, we will need to publish it to a shared repository like the VSTS Git repository.
Figure 4: Status of local repository in Visual Studio
We may have a team project that uses Git, or we can create a new one, and connect to that team project in Visual Studio. Once that team project is ready, we will start the publication of our local repository to the remote repository (that gets created when the team project is created). To link these repositories, we will use the Sync operation.
Figure 5: Publish repository to VSTS Team Project
This operation first will use our authentication credentials to check the available repositories under various team projects for our account in VSTS. It will show all the team projects that use Git, and then allow us to select the team project and the repository under that.
To view the remote repository mapped to the local repository, select Settings > Git Settings in the team explorer.
Figure 6: Git settings and linked remote repositories
Creating a Master Branch and Remote Branch
Let us now step through another scenario. Here we will create a branch of master and then create another remote branch in the same repository. To create a branch of master, we will open the Branches tab in the team explorer. By right clicking on the master, we can create a new local branch. This local branch has all the contents which are in master at this moment. We will now publish this branch so that a remote for it will be created.
Figure 7: Publish a newly created branch
We can create and publish as many branches as needed in the same repository. When we checkout a branch, it becomes the active branch in which we can add edits by commits.
Figure 8: Add a temporary brnach and make commits to it
After the edits are over, we can push them to the remote repository on VSTS.
Git Pull Request Code Review
These pushed changes can be merged into master branch but before this merge, it is a best practice to get the code reviewed by your peers. In git, this process of requesting and providing the code review is implemented as a feature called pull request.
This feature of VSTS is available from the web interface, and not from the Visual Studio. So we open the web interface for the same team project in which we have the repository. Under the Code section of that team project, we will find the subsection for Pull Request. Since this is the first time that a pull request is to be created, the page will show the button to create New Pull Request.
Figure 9: Create new Pull Request
For creating the new pull request, we have to specify which branch should be reviewed, and how does it defer from any other branch. So we compare code between say branch bugfix1, with the code of master branch.
Figure 10: New pull request
The commit and the comment while that commit was made, are automatically detected and accordingly mentioned on the Pull Request form. We can change the description by using markdown code. We can also provide the names of the reviewers.
Reviewers will be able to see the pull request that they have received. They can view the various perspectives to that pull request like, overview.
Figure 11: New pull request overview
Files - that show the differences between the files in selected branches
Figure 12: Pull Request files comparison
Reviewers can add comment in the code via a pull request.
Figure 13: Add comments in code through pull request.
They can also view Updates and commits done in the branch that is under the review.
After the review is done, code can be Approved, Approved with Suggestions, wait for author (to make necessary changes) or Reject the code.
After the code is Approved by the reviewers, the pull request is completed. Completion of pull request merges the selected branches say from bugfix1 into master branch.
This article explored two features of VSTS Git. First, we checked how to add a solution or project to existing repository on VSTS. We then took an overview of pull request features which facilitates code review before the code is merged from a temporary branch into a master or a release branch.