Logging Experiences

Using Git with Github on Windows

Posted in Git by Sina Iravanian on May 5, 2012

If you’re new to Git and don’t know how to install Git on Windows, read my earlier post: How to Setup Git for Windows

In an earlier post, I described the preliminary steps required to work with git on CodePlex. However using git with Github requires more initial steps.

Generating SSH Keys. Before proceeding to Git operations we need to create SSH public and private keys, and register the public key on the Github site. In order to generate the keys we can use PuTTY Key Generator, which is already bundled with TortoiseGit fortunately. This program is called “Puttygen” and can be found in start-menu under TortoiseGit:

puttygen under TortoiseGit in Windows start-menu

After running Puttygen you need to press “Generate“, and make some crazy mouse movements in the spotted area to provide some random data for the program (wow, what a brilliant idea). When finished you will have the public key generated. It is recommended that you provide a passphrase so that you would have a more secure private key. After that save your public and private keys using the 2 save buttons near the bottom of the window. But these are not all that you need to save! The public key format that Github and some other applications require you to provide is not the one that this program has saved for you. The topmost field which is labeled “Public key for passing into OpenSSH authorized_keys file” is the one that Github needs. It should be in one line (i.e., with no line-breaks), therefore we would refer to it later as the one-line-public-key. Please save the contents of this field and the next “key fingerprint” field somewhere for future references.

Puttygen fields

Registering SSH Keys in Github. Browse to Github, and go to your Account Seetings, and select SSH Keys therein. Press the Add SSH key button on the right. Provide a name for the key (I chose TortoiseGit), and paste the one-line-public-key in the Key field. Press Add Key. You may be prompted for your Github account password next. When finished, Github adds this key to its list of keys and provides a fingerprint. Make sure this fingerprint matches the key fingerprint that Puttygen created for you.

Creating a Repository in Github. Browse to the Github page and press the “Create a New Repo” button at the top of the page.

Github Create New Repo

In the new form that appears, enter desired values for your project’s name and description. Currently there’s an option to add a README file or an appropriate .gitignore file to the repository. While making use of these options are recommended, in this tutorial I intend to start from a completely empty repository; therefore I unchecked those options. When finished press the “Create repository” button. With these steps I created a project called “PdfRenamer“.

Setting up TortoiseGit for the new project. After creating the repository, Github introduces some helpful basic commands to start working with the repository. We don’t intend to use the command-line, however this page contains useful information such as user-name and email with which Github recognizes you, and the git repository address, as highlighted below:

Github New Project Address And User-Name

We need to do the following steps next:

  1. Right click somewhere in the Windows explorer window, and from the context-menu go to: TortoiseGit > Settings >> Git branch; and fill the user-name and email fields with the ones suggested by Github, and press OK.
  2. Create a folder preferably with the same name as your project (in my case PdfRenamer), and browse into it with Windows explorer.
  3. Right click somewhere in the folder and from the context menu select “Git Create repository here…“. A window pops-up. Make sure that “Make it Bare” is not checked; and press OK.
  4. Right click somewhere in the folder and from the context menu go to: TortoiseGit > Settings >> Git branch >> Remote sub-branch. In the “Remote” field enter “origin“, in the URL field enter the git repository address which was highlighted above and is in the form of git@github.com:[username]/[projectname].git; and finally in front of “putty key” provide the address to the SSH private key which we saved earlier in this tutorial, and has the .ppk extension. When finished press the “Add New/Save” button. After that origin should be added to the list of Remotes.

TortoiseGit Remote Setting

Add files to the project structure. With git, adding a file to a project structure and having it reflected in the remote repository requires 3 operations: add, commit, and push. The first 2 operations are required for adding the file to the local repository, and the third one reflects the changes to the remote repository. This last operation can be postponed to a later time when you want to push a bundle of changes all at once.

To start, create a Readme.txt file in the root folder. Right click on the newly added file, from the context menu select: TortoiseGit > add; check the files to be added, and press OK. This was the first operation: add. In the new window press “commit …“, provide a meaningful commit message in the new window, and press OK when finished. This was the second operation: commit which reflects the changes to your local repository, the one in your hard-drive. You can push the changes in the next window if you want, however it’s not a good idea to push every local commit to the remote repository (that’s why we are using a DVCS after all).

Before continuing to the push command, let’s talk about some naming conventions in the Git world. With DVCSs such as git you can have different remote repositories which you may choose from to push your changes to. By convention, the default name for the remote repository is “origin“. In our case origin is the remote repository at the Github server, for which we manually added the link before. On the other hand, each project may have several branches. Conceptually a branch is another copy of the same project where other features are developed and tested. For example imagine that at a certain stage of development you may decide to add a new feature to your project. You may create a new branch called “test”, develop and test the feature there, and finally merge the changes with the code at the main branch. By convention, the main branch is called “master“.

When it comes to push, you need to know which branch of the code you want to push to which remote repository. In our simple case we want to push the master branch to the origin repository. For this purpose, right click somewhere on the Windows explorer that shows the project root folder, and select: TortoiseGit > Push…. Make sure the name of the branches for local and remote repositories and the name of the remote repository are selected correctly. More importantly make sure that the “Autoload Putty Key” check-box is also checked, and press OK. Next, the TortoiseGit will prompt you for the private-key passphrase. Note that this is not your Github account password, this is the passphrase that you chose when creating the private key with Puttygen. After finishing the push operation, check the sources in the project’s Github page to make sure the changes have been reflected there. Note that after the first push, the PuTTY authentication agent (Pageant) get’s run in the background, so that you won’t need to enter the passphrase anytime you want to push to Github.

Updating the local repository with the latest changes made by other team members to the remote repository. Well, there are two solutions. The one that I prefer is using git pull. You can easily find this command in the TortoiseGit’s context menu. The pull command, preserves the history of changes made by different people and makes a final merge afterwards. The other solution is git fetch and rebase. The fetch command only receives the latest changes from the remote repository but does not apply them. The rebase command first applies the remote changes to the repository, then applies your changes. Therefore it always seems that you have made the recent changes, while it may not be true. See this stackoverflow question for a better explanation.

Note: At any stage, if git complains about connecting through a null proxy, then you will have to remove proxy settings from the global .gitconfig file. For more information see tip 7 on my earlier post

You may also want to see my earlier post that describes using git with CodePlex on Windows.

Using Git with CodePlex on Windows

Posted in Git by Sina Iravanian on May 4, 2012

If you’re new to Git and don’t know how to install Git on Windows, read my earlier post: How to Setup Git for Windows

On 21 March 2012, the CodePlex team announced their support for Git.

Creating a project with Git source control is very easy in CodePlex and is done with a few clicks at no time. Suppose the project name is CodePlexGitTest. In order to make any changes to the project structure you need to clone the project first. This way you will have your own copy of the repository on your hard disk. To do this the path to the git repository is needed. This can be obtained from the Source Code tab of the project’s CodePlex page, by pressing the Git link/button under “Source Control” section on the right pane, as seen below:

Git Repository Information on CodePlex

In order to clone the project, move to the folder that is going to host the project directory. Right click somewhere in the Windows explorer window and choose Git Clone. In the opened window set the “Url” field to the “Clone URL” value above, leave other options unchanged, and press OK. After prompting for your CodePlex user-name and password, it should now create the folder structure for your empty repository (Note: in case that git complains about connecting through a null proxy, then you will have to remove proxy settings from the global .gitconfig file. For more information see tip 7 on my earlier post).

Now that you have the currently empty project structure at your hard-drive, it’s time to add some files to it. Using git, adding a file to a project structure and having it reflected in the remote repository requires 3 operations: add, commit, and push. The first 2 operations are required for adding the file to the local repository, and the third one reflects the changes to the remote repository. This last operation can be postponed to a later time when you want to push a bundle of changes all at once.

To start, create a Readme.txt file in the root folder. Right click on the newly added file, from the context menu select: TortoiseGit > add; check the files to be added, and press OK. This was the first operation: add. In the new window press “commit …“, provide a meaningful commit message in the new window, and press OK when finished. This was the second operation: commit which reflects the changes to your local repository, the one in your hard-drive. You can push the changes in the next window if you want, however it’s not a good idea to push every local commit to the remote repository (that’s why we are using a DVCS after all).

Before continuing to the push command, let’s talk about some naming conventions in the git world. With DVCSs such as git you can have different remote repositories which you may choose from to push your changes to. By convention, the default name for the remote repository is “origin“. In our case origin is the remote repository at the CodePlex server. On the other hand, each project may have several branches. Conceptually a branch is another copy of the same project where other features are developed and tested. For example imagine that at a certain stage of development you may decide to add a new feature to your project. You may create a new branch called “test”, develop and test the feature there, and finally merge the changes with the code at the main branch. By convention, the main branch is called “master“.

When it comes to push, you need to know which branch of the code you want to push to which remote repository. In our simple case we want to push the master branch to the origin repository. For this purpose, right click somewhere on the Windows explorer that shows the project root folder, and select: TortoiseGit > Push…. Make sure the name of the branches for local and remote repositories and the name of the remote repository are selected correctly, and press OK. Enter CodePlex user-name and passwords which are prompted. Now check the sources in the project’s CodePlex site to make sure the changes have been reflected there.

How to update the local repository with the latest changes made by other team members to the remote repository? Well, there are two solutions. The one that I prefer is using git pull. You can easily find this command in the TortoiseGit’s context menu. The pull command, preserves the history of changes made by different people and makes a final merge afterwards. The other solution is git fetch and rebase. The fetch command only receives the latest changes from the remote repository but does not apply them. The rebase command first applies the remote changes to the repository, then applies your changes. Therefore it always seems that you have made the recent changes, while it may not be true. See this stackoverflow question for a better explanation.

See also:
How to setup Git for Windows
Using Git with Github on Windows

How to setup Git for Windows

Posted in Git, Programming by Sina Iravanian on May 1, 2012

These are some easy steps required to setup Git for Windows:

  1. Download msysGit from: http://code.google.com/p/msysgit.
    I prefer to use the portable version. At the time of this writing there’s no difference between 32 bit and 64 bit versions and the filename for the portable version is: PortableGit-1.7.10-preview20120409.7z. Currently it seems that the development of the project has been moved to Github, but the releases are still located in Google Code.

  2. Extract the contents in a proper location. I made them into:
    D:\PortableProgs\msysGit

  3. If you intend to use the git command-line for every git operation simply run git-bash.bat in the root folder of msysGit. Yes, this is the old lovely Cygwin command-line.

  4. If you don’t intend to use command-line (like me) install TortoiseGit. It is hosted at Google Code:
    http://code.google.com/p/tortoisegit/
    Note that it comes with different releases for 32-bit and 64-bit systems. At the time of this writing the latest version is 1.7.8.0. After installing TortoiseGit you may need to restart Windows, or the explorer process, or none.

  5. Right click somewhere on a Windows Explorer window, and from the context menu select: TortoiseGit > Settings. There will be a message-box appearing begging for adjusting the path to msysGit. Click on “Set MSysGit path” button (If you have ever missed this window, or want to change the path to an already existing msysGit, simply go to: TortoiseGit > Settings >> the General branch).
    In the field titled as “Git.exe path:” enter the path to the bin folder of the msysGit installation/copy.

  6. You don’t have to, but it is highly recommended that before starting any git operations you set some global settings such as your name, email, and AutoCrlf. To this aim in the Windows explorer’s context menu go to: TortoiseGit > Settings >> the Git branch. Fill in the fields labeled Name and Email with proper values. Then make sure that AutoCrlf check-box is unchecked, so that you don’t touch every file in order to change their line-endings. Read more about this kind of problem here, and see here to know more what AutoCrlf and SafeCrlf options do for you.

  7. [UPDATED on 3 May 2012] When performing some git operations, git may complain that it cannot connect through a null proxy. It may happen for some versions if in git settings the value for proxy is assigned to an empty string. If this is the case for you, simply remove proxy settings in the global .gitconfig file. To do this, right click somewhere in Windows explorer, go to: TortoiseGit > Settings >> The Git Branch >> Edit Global .gitconfig button. From there remove the line that assigns proxy, or the whole “[http]” section if it only contains proxy settings.

See also:
Using Git with CodePlex on Windows
Using Git with Github on Windows

Tagged with: , ,