Loading ...

How to use GitHub with Visual Studio | CodeAsp.Net

How to use GitHub with Visual Studio

4.88 
 /5
4.88 (2votes)

Step-by-step Guide on using Github with Visual Studio

Source control is an integral part of software development, and a good source control tool not only increases developer productivity but also helps in managing your software development cycle more effectively. Throughout last 10 years I have used a variety of source control tools, from the "un-scalable" VSS (Visual Source Safe) to SourceJammer, Vault, SVN and TFS. But it was only when I started using Git that I fell in love with a version control tool for the first time! Git rocks, and GitHub (SaaS version of Git) is just awesome. 

To know more about how Git is better than other tools, check out these links: 

http://boxysystems.com/index.php/5-fundamental-differences-between-git-svn/

http://markmcb.com/2008/10/18/3-reasons-to-switch-to-git-from-subversion/

Now here are the steps to follow if you want to quickly jump to using Github for your Visual Studio projects:

1. Download and install Git for Windows (the latest version at this time is: Git-1.7.6-preview20110708.exe): http://git-scm.com/

2. Download Git Extensions http://code.google.com/p/gitextensions/. Git Extensions will help you use Git without using command-line interface and also allows you to integrate Git with VS 2008/2010. Make sure you check the option of installing KDiff editor for merge-compare. Otherwise you can download it separately from: http://sourceforge.net/projects/kdiff3/files/

3. Install Git Source Control Provider for Visual Studio: http://visualstudiogallery.msdn.microsoft.com/63a7e40d-4d71-4fbb-a23b-d262124b8f4c

4. After installing Git SCC, restart VS, and then go to Tools->Options->Source Control, and select Git Source Control provider in the dropdown.

The next steps are only needed if you are the developer who is setting up the project on GitHub. If the project is already set on Github, then move to step 13

5. Create a new account on GitHub, and add your new repository. At this point the repository will be empty.

STEPS 6, 7 ARE ONLY NEEDED IF YOU ARE USING SSH instead of HTTPS. HTTPS is the recommended method for authentication.

6.  Bypass this step if you are using HTTPS instead of HTTPS. Next generate your public/private key pair files. To generate the SSH keys, open Git Extensions, click "Remotes->PuTTY->Generate or import key" option. Click the Generate button, you will need to move your mouse in the empty area till the key generate is completed (to add randomness in the generated key). Once the key is generated, save the public and private key files separately. Also copy the public key string in a notepad because it is needed in the next step. Always store your public and private key files so that you can use them later.

7. Next go to the "Github -> Account settings - > SSH keys" and click Add key option. Then enter your public key string from step 5 above and click save.

8. Create a new Visual Studio project. This project will need to get "pushed" to the new repository you created on Github. Once the project is created, click "Git" from the main VS menu and select "Initialize new repository".

9. Enter your local folder path (where you created your VS solution/project) and click Initialize button.

10. Once the repository has been initialized, go back to the "Git" main menu in VS header, and select "Manage remotes" option. In the Remote repositories tab, enter your repository name, and in the URL field, enter the HTTPS OR SSH URL (you can get this from your main Github repository page), then select your private key file (which you generated and saved in step 6). If using SSH, then click Load SSH button to load this private key file. Once the file is loaded, click Test Connection. A command window should open up saying Test connection successful. If its not successful then either the repository address is wrong or the private key is not matching with the public key added for this repository on Github.com. 

11. Once the test connection is successful, click the Save button. A prompt window will open up asking you to for the default push/pull behavior. Click OK, and close this window.

12. Now you need to "commit" and "push" your project files into the remote repository on Github. To do this, right click your VS project (or solution if you have multiple projects), then select Git -> Commit. To upload your changes, select Push. You will see the standard "locks" in all your project files in VS. Now you can even work in check-in-check-out mode.

Working with exisiting repositories on Github.com

13. If you already have an existing repository on Github, and you just wanted to download it locally and get started, then open VS and select Git->Clone repository. Enter the repository HTTPS OR SSH address (you can get this from your Github repository page), select the local destination folder where the files will be downloaded to. Add the subdirectory name where the code will be downloaded. Note that for HTTPS auth, if you want to avoid entering username and password each time you do a push/pull, the repository URL should be in this format: https://username:password@github.com/username/repositoryname.git

14. IGNORE THIS STEP IF USING HTTPS method for auth. Then click the Load SSH Key button, and enter your private key file path and click the Load button. Note: If you areusing SSH and not HTTPS method and have not generated any SSH keys, then follow step 6. Once you have generated your public/private key files, give YOUR public key (as text) to your repository admin on Github.com so that s/he can add your key in the repository settings. Unless the admin adds your public key in the repository, you won't be able to access the repository remotely. 

15. Once the private key file is loaded, then click the Clone button. Note: If you are getting "The server's host key is not cached in the registry" error, then follow this link to fix it: http://stackoverflow.com/questions/5560150/git-push-origin-master-fails

16. Git will download all your files from the remote repository, and you can immediately start checking out the code!

Commit: Does not publish changes to the remote server, only "commits" locally (in the staging area). So commit means that your code changes are ready to be recorded in the repository.

Push: this actually pushes your changes to the remote repository. So always commit first, then push.

To understand more about commit, push, pull and staging area, refer this link: http://gitready.com/beginner/2009/01/21/pushing-and-pulling.html

 ***UPDATE***

Another version of this guide is available here: http://my.axerosolutions.com/spaces/5/communifire-how-to-amp-user-guides/wiki/view/1457/how-to-use-github-to-work-with-forked-private-repositories

Note: Git Default Ignore Files
To exclude /bin, /obj or or any other file to be pushed on the server, make sure you add their extensions in the ignore list. To do so, click Git->Edit .gitIgnore option from the VS header menu and then click "Add default ignores" button. This will add the extensions of the known ignored files, you can add/edit more if needed.

That's it to Github! Once you have configured your project, always commit first to the staging area, and then push the changes. 

Simple FAQs:

1. How to get all the latest files from the server?

A. Use Git -> Pull. You can decide whether to merge changed files or not in the popup which opens.

2. How to update my changes to the server?

A. Use Git -> Commit. In the pop up, select the files you want to push to the server, and move the to the staging area by clicking the Stage button. Then click Commit to commit locally or Commit & push to push the changes to the server.

3. While "pushing" my changes to the server, I am encountering this error:

To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.

A. You need to do a  "pull" first to merge server's latest  changes with yours, and then do a combined "push". If you have merge conficts, then after pulling Git will tell you about these conflicts and you can resolved them manually using cool editors like KDiff3. Make sure that after you do a pull, do not select "Solve Merge Conflicts" as you would want to resolve such conflicts manually.

Comments (2)

   
OmSingh
Hello Vivek,
I found this post very helpful indeed. Thank you for that. I however have a question. Following this post I was able to create repo and was able to push them there. Everything was good.
Then I created one more repo, initialized it as mentioned in steps 8 and 9. Then I tried to set the remotes as mentioned in step 10. I tried using the private key file that I used for first repo, it failed and said "Unable to open connection: Host does not exist". I created another private and public key as mentioned in step 6 but it failed again with same message. What am I doing wrong? Could you please help. Thanks in advance.
6/9/2012
 · 
by
   
OmSingh
It a very trivial mistake I was making. I was trying to push the new local repo using HTTPS url instead of SSH url. I used SSH url and everything is cool now.
6/10/2012
 · 
by

Top Posts