Visual Studio 2012 Using a git Repository

Posted on Thursday, April 25, 2013




I have recently been working on a project using C# and .Net.  I decided to go ahead and buy Visual Studio 2012 Professional.  So far I like it but I have not had time yet to set up a proper repository for it.   I was planning on using git, but I figured Microsoft would not support it directly and I would be forced to use git on the command line (which I am fine with).  Well it turns out Visual Studio 2012 does support git.

I found some information on the fact that Visual Studio can use git at this site. http://www.hanselman.com/blog/GitSupportForVisualStudioGitTFSAndVSPutIntoContext.aspx [1]


This document does not go over TFS (Team Foundation Server), although you can use git with TFS.  Also you can use git with  Team Foundation Service https://tfs.visualstudio.com/en-us/, its free for up to 5 users (not sure if it's going to remain that way)





Update to Visual Studio 2012



Before you can go any further you need to update Visual Studio to Visual Studio update 2  (date published 4/4/2013)

Go to this site and download the update http://www.microsoft.com/en-us/download/details.aspx?id=38188 [2]




Click on Download


Run the installer





Make sure you have some disk space looks like it takes up another 816 MiB.    Check Box the agree to license and click Install




Now let it start downloading and update….





Set up successful!  In my case it took about 30 minutes to complete.  Click Restart Now.


After your computer restarts open up Visual Studio





On the "HELP"   Menu tab select "About Microsoft Visual Studio"




Here you can see the Update 2 has been successfully installed.




Download Visual Studio Tools for Git







Click Download


After the progam downloads run it.




Accept the License and click Install




Click Finish



Restart Visual Studio




Set up a remote git Repository


I am going to set up a git server on an Ubuntu 10.04 server.  This will be for the purposes of having a Remote Repository.

Create a git user


> sudo useradd -d /home/git -s /bin/bash -m git
> sudo passwd git

Create ssh keys for git user


> su git
> ssh-keygen -t rsa -b 2048
> touch .ssh/authorized_keys



Add authorized keys for each user you want to use this git server.  In my case I was lucky enough to already have the keys I need under another user (my own)


> su patman
> cd
> sudo cp .ssh/authorized_keys /home/git/
> su git
> cd
> cat authorized_keys >> .ssh/authorized_keys




install git



> sudo apt-get install git-core



Now if you run this command


> which git


You should get







Change the shell for git


You do not want people to log in as a the git user.  To fix this you need to set the shell the git user uses to "/usr/bin/git-shell".  To do this run the following command.



> sudo chsh -s /usr/bin/git-shell git



Now if you try to log in as the git user


> su git


You get this error.




You could still log in as this use if you add the shell in the command like this


> sudo su -s /bin/bash git






Create a location to put the git repositories


Run the following commands to create a location to save Remote Repositories


> sudo mkdir -p /opt/git
> sudo chown git:git /opt/git



Now place a “bare project” in this folder for each of projects you want this server to act as a origin master for.  More information can be found here on how to do this http://git-scm.com/book/en/Git-on-the-Server-Getting-Git-on-a-Server [3]

Assuming you have a git project called my_git_project.git



> sudo su -s /bin/bash git
> cd /opt/git
> mkdir my_git_project.git
> cd my_git_project.git
> git --bare init


From my understanding the --bare init creates a shell git project for you to later push to.

This git “origin master” server should be ready to go!



Create a local git Repository  in Visual Studio


I have a project I am currently working on in Visual Studio.   I want to open it add it to a local git repository then push that repository up to the origin master.   The push to origin master is not necessary, but I like to have an off site repository backup.

Open your project in Visual Studio 2012 update 2.





Right click on the Solution and select "Add Solution to Source Control"



Select Git and click OK.





I got this error " Can not find Name and Email settings of the current user in Git configuration."   Well maybe it's not so much an error as a notification.





git settings in Visual Studio


From the Menu Bar select View and click on "Team Explorer"




Click on Settings




Click on Git Settings





Enter your User Name,  Email, location where you want the repository and Checkbox the Enable download.  (this allows it to get your gravatar image.  If you have not set up a gravatar image set one up now at https://en.gravatar.com/)

Then click Update





Again, Right click on the Solution and select "Add Solution to Source Control"

This time it adds all the files to the Local Git repository.





On the Team Explorer Window.  Click on Home, then click on Changes.






Enter in a comment.  I entered in "First Commit"
You can see that this commit will have 196 changes associated with it.






Click Commit.




Success.  You should see the message Commit XXX successfully created.







Now there should be little blue locks next to each file now in git.  If you hover over it you will see the pop up that says its "Checked In"





If you edit a file, and have not yet saved it you will see this red check mark which, when hovered over, will say pending edit.







Once you are done with the file and have saved it.  Right click on it and select Commit.




Going back to the Team Explorer Window you will see that the file has been added to the next commit.




Install Command Line git tool


To use git its very useful to have the git command line tools.  To install them do the following.




From the Team Explorer click Settings



 

Click Install 3rd Party Tools



 

Click Install


 


This will download a file called msysgitVS.exe

Double click on it and run it.







Click Install



 Click I Accept



Let it install




Click Finish




Click Exit.






To test it click on Actions --> Open Command Prompt




Run the command "git --version"



It should display the version of git you are running





Adding a remote origin


This is working well so far, but I want to have it push the code up to the repository I made on an ubuntu server (in the cloud)



On the Team Explorer click on the Home button and click commits.




Enter in the location of the Empty Git Repository and click Publish.



Error Issue




I got this error "An error was raised by libgit2. Category = Net (Error).This transport isn't implemented. Sorry"

I think because I am using ssh for my remote server and I need to do some more set up to make that work




Click on Open Command Prompt
Run the following commands.



> git remote -v
> git push origin master


The first command will list the remote servers associated with this repository and the second will attempt to push the local repository to the remote repository.





In my case the push fails due to ssh keys.




Fix ssh issues


All I had to do to fix this issue was copy my .ssh file to my windows user's home directory.

From Cygwin I ran the following command



> cp -r .ssh /cygdrive/c/Users/patman/




Now run the following command



> git push origin master



Success!




Although I did need to fix my SSH keys the error is legit.  There is a feature with pushin to the origin server that does not work with the Visual Studio IDE.

So for now you are forced to use the command line push.  Whenever you want to sync with a remote repository





Testing it all out




Right click on any file in the repository and select View History





The repository history for this file is displayed.  For some reason my gravatar image is not being displayed… I am not sure why.






Edit a file, save it, then right click on it and click Commit.



On the team Explorer click on Changes




Enter a comment for the commit and click Commit




Success!






Open the command line tool again and run the following command



> git diff master origin/master



This will display the differences between the local repository and the remote repository.

There should be a difference.

From the command line push to the remote server



> git push origin master




At this point I would like to test using a remote repository to download a project in Visual Studio, but I think I have done enough with this subject for today.


References
[1]        Git support for Visual Studio - Git, TFS, and VS put into Context            http://www.hanselman.com/blog/GitSupportForVisualStudioGitTFSAndVSPutIntoContext.aspx
                Accessed 04/2013
[2]        Visual Studio 2012 Update 2
                Accessed 04/2013
[3]        Visual Studio Tools for Git
                Accessed 04/2013

No comments:

Post a Comment