DotNetCurry Logo

Git Integration in Visual Studio 2012 after Update 2

Posted by: Suprotim Agarwal , on 4/8/2013, in Category Visual Studio, VSTS & TFS
Views: 77441
Abstract: With the release version of Visual Studio 2012 Update 2 out, let's get our hands dirty exploring the Git integration in Visual Studio

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

 

Pre-Requisites

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.

 visual-studio-update-2-location

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.

 visual-studio-for-git-setup

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’

 add-to-source-control

- You get to select the Source Control options as shown below. In our case, we’ll select Git.

 select-git-as-source-control

Once you hit OK, Visual Studio 2012 will create a new Local Repository for you in the same folder as the project.

new-git-repo-created

- Now you can right click at the Solution level and Select ‘Commit…’

 commit-to-repo

- 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).

 select-to-exclude

- 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.

 excluded-packages

- 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.

 all-files-comitted

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.

new-empty-repo

- 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.

git-credentials

- 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.

publish-completed

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

new-initialized-repository

..you can start up Visual Studio, go to the package manager console and download the Repository first using the following command

git-clone-from-console

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.

new-source-in-cloned-folder

- 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.

Other Goodies

Visual Diff

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

image

History

Similarly you can select any file and invoke it’s history from the Solution Explorer’s ‘History’ menu item

history

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.

TFS Integration

connect-to-servers

You can also get Gravatars based on Email address provided, to see user Gravatar against check-in details.

Wrap Up

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.

Was this article worth reading? Share it with fellow developers too. Thanks!
Share on Google+
Further Reading - Articles You May Like!
Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by peter.schabell on Tuesday, April 9, 2013 6:30 AM
Thank you Sumit Maitra/Suprotim Agarwal for the step-by-step instructions. I could commit my first file to our repo successfully!
Comment posted by Subodh Sohoni on Thursday, April 11, 2013 7:02 AM
Very good article Suprotim and Sumit! Hope to see more articles from you on TFS!
Comment posted by Dean on Thursday, April 18, 2013 3:14 AM
nice article, can this work with any flavour of git e.g. stash?
Comment posted by YW on Monday, May 27, 2013 8:48 AM
Thank you!! Very helpful.
Comment posted by James Brown on Wednesday, June 12, 2013 11:20 PM
Nice step by step for a nubee like me.  I have a problem finding the URL of the repro from GitHub.  How do I find that?
Comment posted by lbu on Friday, August 30, 2013 7:59 AM
Nice article... The git command does not seem to be included in the package Manager console though
Comment posted by Bruce Martin on Sunday, September 8, 2013 8:01 AM
My output after selecting the GIT Source Control option is:

An error was raised by libgit2. Category = Os (Error).
Failed to rename lockfile to 'C:/Users/Bruce/Dropbox/Documents/Visual Studio 2012/Projects/GRDBweb/.git/config': Access is denied.

So Visual Studio 2012 did not create a new Local Repository. Now what?