As we all know, Visual Studio 2012 Update 2 went live a couple of days back. As with each official release, it has a host of enhancements to your favorite IDE, but for me it was important for one more reason. With the Release version of Update 2 out, I could now go ahead and try out Visual Studio Tools for Git.
This article was co-authored by Sumit Maitra and Suprotim Agarwal
Some side-notes on Visual Studio 2012 Update 2
Before we get started, you obviously need Visual Studio 2012 Update 2 itself. You can get the web installer here. However if you (like me) use Visual Studio on Multiple computers, downloading the entire update more than once is a terrible waste of time and bandwidth. So you can download the ‘offline’ version by simply running the web installer from the command prompt with the /layout command. So if you downloaded the installer to your Downloads folder, open up a Command Prompt and fire away the following
C:\Users\[replace with your user id]\Downloads>VS2012.2.exe /layout
This will launch a UI asking you to specify the folder where it should be saved. By default, it creates in My Documents with the following folder name.
Once the entire thing is downloaded (it is about 1.8 Gigs in total), you can start the VS2012.2.exe in the above folder to complete the installation.
Install Visual Studio Tools for Git
This one is easy, just download the extension from Visual Studio Gallery here. It is a .msi installer file as opposed to the standard VSIX. Install it with default settings and you’ll see the following once the installation completes.
From Visual Studio 2012 to a Git Repository
Git as we know is a distributed Version Control System (DVCS), so the concept of local repository is built in. You can simply start a repository anywhere. This might sound a little alien to people coming from Non-Distributed Source Control systems like SVN or Visual Source Safe. Thanks to this ‘Local Repository’ concept, we can use Git in two ways.
Starting with Local Repository and publishing to Remote Server
As the title implies, we first create a local repository and check in code locally. Note that you can checkin (commit), update code/build/test, checkin (commit) again and continue doing this. Git will maintain versions at every checkin level. Once you are satisfied with the status of the project, you can then ‘push’ the entire set of (all the) changes to the remote repository (server). That’s the beauty of DVCS.
Let’s get started.
- Create a new Visual Studio Project (I’ve taken an MVC, Internet Template project here).
- Right Click on the Solution Node in the Solution Explorer and Select ‘Add To Source Control’
- You get to select the Source Control options as shown below. In our case, we’ll select Git.
Once you hit OK, Visual Studio 2012 will create a new Local Repository for you in the same folder as the project.
- Now you can right click at the Solution level and Select ‘Commit…’
- This will take you to the Team Explorer and show you all the changes that are ready for commit. For the first time around, you’ll see the entire solution (each file has a + next to it, indicating it’s been newly added and not yet committed to the repository).
- Be careful here and exclude the bin and packages folders. Select the node and click on the ‘Exclude’ link to move it to the ‘Excluded Changes’ section.
- Note above that the Commit Message is required and you cannot Commit/check in without it.
- Provide comment and click Commit to push the changes into Local Repository. If you look at the solution explorer, now you’ll see all files with a blue lock indicating they are checked in.
Pushing Code to Server
Now that we have got the code committed in our local repository, we may choose to continue working and committing code as we deem fit till the point we feel that the code now needs to be sent to a remote server (could you your corporate source control system or a public source control system like GitHub or TFS Online).
- Just like any Source control system, an ‘admin’ has to ‘create’ the ‘empty’ project on the Server and give you access. Today I am using dotnetcurry’s Git repository on Github to create the following new project.
- Note the ‘Initialize this repository with a README’. DO NOT check this for VS plugin to work.
- Once the project is setup, in Visual Studio, select ‘Team Explorer’ and click on the ‘View Commits’ to see the list of all commits. For the first time, you will see a message saying no Remote Configured. Copy the URL of the repo from GitHub, paste it here and hit Publish.
- First time around, Visual Studio will pop the above dialog to get your GitHub credentials. Once the update is complete, you’ll see a screen similar to the following. Congratulations, you code is now in GitHub.
Starting with a New GitHub project
If you like to start off with a remote project directly, then create the new project on GitHub and download it using Visual Studio’s Nuget Package Manager Console. YES, the Git Integration includes the powershell scripts required to be used from the Console. So if you created a project like the following
..you can start up Visual Studio, go to the package manager console and download the Repository first using the following command
Note that you’ve to provide the full path. Default puts it in the C:\Users\[user id]\[repository name]
- Now that the code is downloaded, simply start up a new project and place the solution in the above folder to start making use of the Git Plugin.
- When the Project opens up, you’ll notice in Solution Explorer the files are already marked with a Green +, indicating newly added. You can go ahead and Commit or Publish to the remote repository using the same steps as shown above.
NOTE: One thing that’s likely to be a bug is that when we clone the project from GitHub, it gets a .gitignore file from the server. However when you try to commit, it ignore the settings in this file and you’ve to exclude the bin, and packages folders again before you commit. The Plugin has not reached 1.0 yet so I am hoping this will get fixed soon.
Of course DIFF is built in, how can we miss that. You can right click on any changed file and select “Compare with unmodified” to see a side-by-side comparison
Similarly you can select any file and invoke it’s history from the Solution Explorer’s ‘History’ menu item
I have not touched upon the TFS integration aspect in this post, fact is you can connect to TFS host Git Repositories as well and see them from the Team Explorer’s ‘Connect’ pane.
You can also get Gravatars based on Email address provided, to see user Gravatar against check-in details.
This post could have waited till the 1.0 release but personally I’ve been looking forward to it rather impatiently and the release of VS2012 Update 2 cleared the only barrier I had from deploying it on my Dev machine.
The Git plugin is pretty nifty and seems to have the basic functionality baked into the GUI. Looking forward to using it on a regular basis.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!