13 April 2014

Pushing to GitHub without the authentication problem

After creating a repository in GitHub, accessing it and pushing to it from Fedora with Smartgit and an authentication token, I found it very perplexing that i wasn't able to do the same thing from Windows. Here, whenever I tried pushing to the repository, Smartgit gave me an authentication error. If anyone else is going through the same problem, this is the way to solve it:

Download and install Git for windows: http://git-scm.com/downloads

Open the Git Gui and select "Show SSH key"

Click on "Generate a key" and copy that key to the clipboard. This is the key with which Github will know that a connection from your PC is genuine. You'll be asked to enter a passphrase. Remember this passphrase, because everytime you want to push to the repository, you'll be asked for this passphrase.

Open your GitHub account and navigate to the account settings > SSH keys > Add SSH key
to add the key that you copied to the clipboard.

I initially tried using PuTTY's plink.exe (there's a GIT_SSH system environment variable which had first pointed to just 'plink' and I changed it to the full path to C\Program Files\PuTTY\plink.exe, (but the right way to do it is to point it to the ssh.exe of the Git installation, after which the Git GUI's will work fine)

but kept getting a  "The server's host key is not cached in the registry" error) to do the ssh authentication. Then decided to unset the GIT_SSH variable. Just open up the Git Bash prompt in Windows, and type "unset GIT_SSH" (without the quotes).

That's it! Now you'll be able to clone and push to GitHub.

To clone:
First, copy the SSH link
Now use that link in Git Bash like so (attribution):

git clone git@github.com:yourgithubusername/yourrepositoryname.git

The remote repository will now be cloned onto your local PC.
Create a few new files in the local repository and add them to the 
local repository by typing:

git add .
This will add the new files to your local Git repository (note the 
space and the dot after 'add'). Commit by:
git commit -m "some message"
and push to the remote server using:
git push
It's as simple as that! Somehow, I liked the process of doing it via the 
commandline more than via a GUI.

At this point, you'll be able to push from SmartGit or from Git GUI too.

At any point of time if you wish to see the files which contain your 
ssh authentication, go to the Git Bash prompt and type:

cd ~/.ssh

and you'll see the rsa files that hold your ssh keys. If you want to 
generate new keys, you can always delete these files and create new ones 
as I showed in the beginning of this blog post.

No comments: