Wednesday, March 14, 2012

Git for Windows with TortoiseGit and GitHub


For this project, we are required to use Git for version control and host code on GitHub. As a person who has always preferred SVN (for its linear/incremental nature), and have only had experience with Google Code and SourceForge, setting up a new GitHub repo was a new experience. And so, for future reference, I document the process here. In this tutorial, we install msysgit and TortoiseGit and host code on GitHub.


Installing TortoiseGit:
http://code.google.com/p/tortoisegit/ (yes, its hosted on Google Code)
TortoiseGit is a shell extension for Windows Explorer that allows for easy management of Git projects. Its a port of the popular TortoiseSVN project, but for Git. First step would be installing Git for Windows (msysgit), which will also give you a nice, simplistic bash shell that you can work with (and execute the above commands just like on Linux if you'd prefer that over TortoiseGit). I personally just downloaded and installed Git 1.7.9 with all default settings. Next was installing TortoiseGit itself, which should autodetect the installed Git setup (though you can configure it later if you haven't) and add itself to the Windows Explorer right-click context menu. If it all worked nicely, you should see something like this:

Windows Explorer context menu with TortoiseGit (and TortoiseSVN) installed

Generating your public key:
To associate your computer with GitHub, you will need a "public key/private key" pair. You could do this entire process manually with the bash shell, but TortoiseGit gives you a handy GUI tool for it: Puttygen (PuTTY Key Generator). Run Puttygen via your Start menu: "Start -> Programs -> TortoiseGit -> Puttygen". Click "Generate", move your mouse around the blank box for a bit, and wait for the box to fill with your newly generated key. Copy the entire text in the box (right-click -> Select All, right-click -> Copy). This is your public key. Then click "Save private key" and choose a safe place to store the .pkk file (e.g. "TortoiseGit.ppk"), passphrase optional. This file stores your private key.

Run PuTTY Key Generator, copy the public key to your clipboard, and save your private key

Adding your private key to TortoiseGit:
Next you want make your private key accessible to TortoiseGit for authenticating with GitHub. For this, we'll manage our key list with Pageant: "Start -> TortoiseGit -> Pageant". This will add an icon to your taskbar (see picture below). Right click the icon and select "View Keys". Click "Add Keys" and navigate to your previously saved PuTTY key that is storing your private key file (e.g. "D:\Development\Sandbox\TortoiseGit.ppk"). If added properly, you should see the new key added to the list.


Run Pageant and add your private key file.


Registering for GitHub:
https://github.com/signup/free
GitHub is a project hosting website that focuses mainly on Git. As of the moment, they offer free public repositories and unlimited public collaborators for open source projects. Registration is pretty straightforward - just sign up for a free personal account.

Make sure to choose "Create a free account"

Adding your public key to GitHub:
https://github.com/settings/ssh
Go to your "Account Settings" on GitHub and go to the "SSH Keys" section. Click "Add SSH key" and paste your public key into the box. Give the key a descriptive title (it doesn't really matter) and then click "Add key". After confirming your GitHub password, you should see the new key added to your "SSH Keys" list.

Add your generated public key to GitHub

Creating a GitHub repository:
https://github.com/repositories/new
Go to your GitHub profile page (click your username on the top right) and click "New repository". Proceed to fill out the project information. Note that the "Project Name" becomes the repos name, so you may want to use a clean, space-less name (e.g. "DancingMonkeysAccelerated" for this project). After clicking "Create repository", you'll be presented with a list of commands to execute to create your first commit. Since we'll be using TortoiseGit instead of a command-line to manage files, you can ignore

Go to your profile page and click "New repository"
Copy the URL for your Git repository (you can ignore the rest of the page)


Creating your local repo:
Now that the project has been created on GitHub, its time to create it on your computer! The GitHub page will give you bash commands for creating the new repos, but we'll use TortoiseGit instead. Navigate to the folder/drive where you want your code to be stored (e.g. "D:\Development\Sandbox"). In that folder, right click to open up the Windows Explorer context menu and select "Git clone...". In the popup box, copy-paste the Git repo URL from GitHub (see above picture) and click "OK". Since you've already set up the authentication earlier and its an empty repository, it should be quick (the log will warn you about cloning an empty repo though). If a new folder has been created with the name of your project (e.g. "D:\Development\Sandbox\DancingMonkeysAccelerated"), the clone was successful!

Clone your Git project to your local computer
Push files/changes to your local branch:
What you have now is a clone of a branch of your repository. Your current branch is probably the master branch assuming you just created a clean project. Go ahead and add files and your code to the folder. Once you're happy with your code/changes, it's time to commit these changes. You can do so by right clicking to open up the context menu in the clone's folder and selecting "Git Commit -> "master"...". This will open up a window that will allow you to add the change message and select what changes you want to commit. If its your first commit, you will want to check the "Select/Deselect All" checkbox to add all your new files. Click "OK" when you're happy to commit the changes to your branch. Note that unlike SVN, this commit is LOCAL ONLY!

Committing your changes to your local branch


Synchronizing changes with GitHub:
After a few commits, you might want to push your changes to GitHub. Open up the context menu in the clone's folder and selecting "TortoiseGit -> Push". In the new window, if you also added tags, don't forget to check the "Include Tags" checkbox before clicking "OK". If other project collaborators pushed out changes, you can pull their changes from GitHub with "TortoiseGit -> Pull".

Pushing your local commits to GitHub

Associating your private key with the git repos:
To avoid having to manually start up the Pageant daemon every time you want to sync your git repos with GitHub, you can add your PuTTY key to TortoiseGit's Remote settings. Open up the context menu in the clone's folder and selecting "TortoiseGit -> Settings". In the new window, select "Git -> Remote" and select your Remote item (in my case, "origin"). Locate and select the .ppk file from earlier for the "Putty Key" field and click "OK". Now you don't have to manually start up Pageant every time you reboot your computer!

Associating your PuTTY key with your git repos

You're done!
And that's it! If you check your GitHub project page, you should see your latest commits and navigate your code's different branches and tags. For this project ("Dancing Monkeys: Accelerated"), the project page can be found at https://github.com/Keripo/DancingMonkeysAccelerated

It works!

21 comments:

  1. Great tutorial. This will be useful for future CIS 565 students too.

    ReplyDelete
  2. Nice tut man. I was lost before this.

    ReplyDelete
  3. Thanks for this tutorial! it really helps

    ReplyDelete
  4. Much, much better than this one: http://stackoverflow.com/questions/370030/why-git-cant-remember-my-passphrase-under-windows

    ReplyDelete
  5. One new addition. In "Associating your private key with the git repos" you should add important notice, that user have to change repo's URL from HTTP to SSH one, or else password-less, SSH-keys based, login will fail. This is important, because many new GitHub users uses HTTP URL by default, not SSH one.

    ReplyDelete
    Replies
    1. Yes. I had this issue. Changing the origin URL from HTTPS to SSH helped me to make it work. Thanks

      Delete
    2. Thanks for this one. I missed this one, hehe. Now, I am using the SSH url.

      Delete
  6. Thank you for this tut. Its the only one, out of several tried, that actually worked.

    ReplyDelete
  7. Your website will service as your business ID on the Internet and you and your business will be judged by the look and functionality of it. Cheap Hosting

    ReplyDelete
  8. Thanks for your tutorial. It helps a lot for my first Github project.

    ReplyDelete
  9. Nice blog and good content .. Now get your own domain and hosting in just $1 /Month hosting. https://www.hostripples.com

    ReplyDelete
  10. can you make a guide like this also for bitbucket?

    ReplyDelete
  11. Thanks... ufff. This tutorial you helped me.

    ReplyDelete
  12. Git and Github Online Training
    www.21cssindia.com/courses/git-and-github-online-training-248.html
    Familiarization with Git and DVCS concepts; Introduction to the Github platform; Repository creation on the web and desktop; Document versioning with local ...
    Call Us +919000444287 or contact@21cssindia.com

    ReplyDelete
  13. When I clone "load putty key" is disabled for me (not github repo). And TortoiseGit always asks me for passphrase for my private SSH key. If I ran git clone/push/pull in console ssh-agent stores me passphrase (like mentioned here http://help.github.com/ssh-key-passphrases/) and do not ask me for it every time.

    ReplyDelete
  14. Awesome...Great. Saved my Day. Great Tutorial!!!

    ReplyDelete
  15. Free easy & simple way to learn programming online we provide niit projects, assignments, cycle tests and much more..
    visit ====>>> https://githubhelp.blogspot.in

    ReplyDelete