Popular blog tags

Using Bitbucket for Git in Visual Studio 2017

Published
http://www.dotnetcurry.com/visualstudio/1374/using-git-bitbucket-with-visual-studio-2017 Abstract: Connect to a Bitbucket from Visual Studio 2017 and use Git features to create and clone a repository, commit, create and merge branches and create and use pull requests.           Some time back I received a requirement for Git training. Later it turned out that the customer was interested in learning Bitbucket and that too with the command prompt. Ofcourse using Git commands via command prompt is a little tedious, as compared to using the wonderful IDE of Visual Studio.  Download 100's of Visual Studio .NET Controls for ASP.NET, MVC, Core. Try for Free. Considering many of you are Visual Studio users, in this article, I will discuss how to create a Bitbucket account, how to add its extension to Visual Studio and how to use various Bitbucket commands like, commit, commit and push, commit and sync, pull request, merge etc. with the help of Visual Studio 2017. Bitbucket Overview Bitbucket is an Atlassian tool for source and version control. It uses either Mercurial or Git. It is a web based service which is available for free up to five users (very similar to Visual Studio Team Services or VSTS). Bitbucket is also available on-premises. Bitbucket supports Git which is a distributed version control system. With distributed (decentralized) version control system, every user can work with his/her own working copy without impacting any other installations on the cloud. Each user can maintain their own repository that has all the versions of the software that is under development and when required, merging with other repositories is possible. There is no requirement of a network as every user is working with local working copy. Bitbucket is useful as it gives you as many number of private repositories as required. These repos are free, Git provides us as many number of public repositories, but not private. If you are looking for open source development, you need to choose git with Github. Bitbucket provides all the features that git supports. Bitbucket also provides project management tools as it comes as a part of Atlassian products. Integration to Jira is available. Bitbucket provides shared repositories amongst team members who are working on a project. In any case, if the teams are using centralized version control system and shifting to distributed version control system, they do have a steep learning curve. Bitbucket Account Creation Let us create an account for Bitbucket. Go to  https://www.bitbucket.org and click on the ‘Get Started’ button. Provide your email id on the following screen. Later provide a unique name and password for the account. After successful creation of your account, you can login to Bitbucket where the screen looks as follows: Adding Team Members Now that we have created an account, we can start working with repositories. Create a team with whom you can access and manage multiple projects or repositories. A team allows you to share your team’s repositories, create projects, allow certain permissions etc. A team can own a repository, a team can have two kinds of groups admin and developers. Once a team is created, we can add users to it. A new team member will get an email stating that he/she has been added and been given permissions as per the user type. Once a team is in place, we can create a project. A project can contain multiple repositories. You can also create an individual repository and send invitations to team members to join in. The team member can join with read/write/admin access. He/she will receive an invitation email. Once a repository is created, we can either add existing code to it or add code from scratch. A repository can be private or public. A private repository is visible to only the team members or to the users who were sent invitations to work with you. A public repository will be visible to everyone. Creating a new Repository Let us start by creating a repository and see how source code can be added to it. Add Bitbucket Extension to Visual Studio Now that a repo is ready, let us find out how to connect to this account using Visual Studio 2017. Go to Tools > Extensions and Updates > search for bitbucket extension in Online tab. Download and install the extension. You need to restart Visual Studio after installation of this .vsix file. Once the extension is installed, you will get the Login to Bitbucket in Team Explorer. We can login by entering user name or email and password and connect to our Bitbucket account. Now clone the repository on our machine. You get Clone as the option in the Team Explorer for Bitbucket account. Clone the repository Cloning is nothing but copying of a repository where the entire working copy is available for Git repository. It will have its own history, files and all required features. You need to specify the local folder while cloning the repository. The folder structure looks as follows after cloning. Start Writing Code In Team Explorer, click on New to create a new project/solution and add code to it. Ensure that you un-check the check box for Create new Git repository from create a new solution (as we have already used Bitbucket git). Commit Options Add a console application to the solution using Solution Explorer. Write some code and you will see there are three options to commit - Commit All (only to local repo), Commit All and Push (local as well as remote repo) and Commit All and Sync (to remote and also fetches changes from remote if any). Provide a comment and select Commit All. You can confirm that there is no code committed to the remote repo by using browser. Add another line, and this time select Commit All and Push and verify from browser. Branching and Merging Branches: Branching is a very common feature when we have to do parallel development. This way, the changes can be kept separate. Bitbucket provides a master branch and we can create multiple branches in local repository as and when required. Branching will usually be followed by merge to the truck or master branch. Forking is a way of creating a branch but that will not merged. We can observe the master branch by going to Branches in Team Explorer. A new local branch can be created by right clicking on existing branch and providing a name to it. We can find the information about the branches like who created it, when was it merged, if not merged, how much is it lagging behind etc. Fetch and Pull: In both fetch and pull, the repo is downloaded from remote repo. Fetch is used to update remote tracking branches, it doesn’t integrate with working files. It helps a repository up-to-date but we can keep on working on something else. Pull merges into the current branch. Pull is a combination of fetch followed by merge. Pull Request When a developer is working in a dedicated branch and creates a specific feature he/she can create a pull request to get feedback, reviews from peers. The developer then creates a pull request via Bitbucket. The other team members then review code, discuss it and can change if required. It is essential to have a branch for creating a pull request. You can add reviewers(s), and the pull request automatically specifies the changed files. If there are any open pull requests, they are also shown. We can set certain features like which tool to use for merge and what files should be added to .gitignore in following manner. Files which are not to be tracked will be ignored. You can put patterns in .gitignore files which will be ignored in git. Observe that I have set Visual Studio for Diff as well as Merge. In case of any conflict, Visual Studio will show the conflict and we can resolve the conflict before commit. Conclusion In this article, we discussed how Bitbucket can be used for Git features. We discussed how we can connect to a Bitbucket account with the help of Visual Studio 2017. We discussed how to create a Bitbucket account, how to create and clone a repository, how to commit, create and merge branches and create and use pull requests. Download an ASP.NET MVC CMS - Free Trial .