Monday, February 28, 2011

Eclipse Egit + github - A quick guide to creating, configuring, pushing and pulling from a github repository.

It does sound simpler than it actually is, but configuring eclipse and Egit to work with github has quite a few bumps. This is meant to be a quick guide and focus on the problems along the way instead of the process, since there are already good guides out there that focus on the entire process (see the references in the bottom, specially [3]). For the record, I'm using:
  • Eclipse 3.6 (codename Helios)
  •  Ubuntu 10.10
This post will cover the following:
  1. Create a repository on github;
  2. Install git;
  3. Configure ssh keys.
  4. Install Egit in Eclipse;
  5. Configure Eclipse;
  6. Push your current project to github repository;
  7. Check out (pull) project from github.
Create a repository

This is fairly straight forward. Go to your account on http://github.com/, click on "Dashboard" (top right) and then "Create a repository". Just follow the simple instructions and you'll get an address for the repo that you'll use later on.

Installing git

That's pretty easy. Just:
$ sudo apt-get install git
You can also install git-doc and git-gui if you like. You should also set your username and email to properly sign your commits:
$ git config --global user.name "your name"
$ git config --global user.email "youremail@yourdomain.com"
Configuring ssh keys

Github provides a pretty good tutorial on generating ssh keys [1], but I'll reproduce the main steps to make it work. Refer to [1] for a more detailed explanation.

Create .ssh folder:
$ mkdir ~/.ssh
 If the directory already exists, it will simply throw an error and skip. Generate rsa key pair:
$ ssh-keygen -t rsa -C "youremail@yourdomain.com"
The -C is just an optional comment. By default, it will be saved in ~/.ssh/ directory. For now leave the passphrase empty! This is important, since Eclipse has a bug handling passphrase protected keys. We will give it a passphrase later. Now, go to your account on github, go to "Account Settings" (top right), than click on "SSH Public Keys" on the left menu and "Add another public key". The tittle should be some comment that identifies which computer this key is from (such as "Home-desktop"), and the "key" should be the exact contents of the file ~/.ssh/id_rsa.pub. There is a neat trick using xclip to copy the contents of the file to the clipboard without messing anything (such as adding whitespace and newlines):
$ sudo apt-get install xclip
$ cat ~/.ssh/id_rsa.pub | xclip -sel clip
Those commands copy the contents of the file to your clipboard. Now just ctrl+v in the "Key" box in github and you should be done. To test everything is working, try running:
$ ssh git@github.com
 And you should receive a message that looks like:
Hi user! You've successfully authenticated,
    but GitHub does not provide shell access.
                                               Connection to github.com closed.
One possible problem here is getting a message that looks like:
Permission denied( publickey).
If you do, try manually adding your generated key to ssh path and checking permissions are correct:
chmod 700 ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa
I had this problem because I regenerated the pairs a few times and ssh somehow couldn't find my key anymore. If that doesn't work, please refer to git troubleshooting guide [2] for other possible problems.

Installing Egit

If you are running Eclipse 3.6+, you can install Egit through the marketplace. In Eclipse, go to Help -> Eclipse Marketplace...  , search for Egit and install it. Restart Eclipse afterwards. If you are not using Eclipse 3.6+, refer to Egit documentation for your eclipse version [4].

Configuring Eclipse

Here, we need to configure SSH.
  1. Go to Windows -> Preferences and filter SSH. Select SSH2 on the left tab (should be under General -> Network connections). 
  2. Check that SSH2 home is set to /home/<yourusername>/.ssh and id_rsa is in Private Keys
  3. On Key Management tab, click on Load Existing Key... and select your id_rsa (private) key under ~/.ssh/id_rsa. If your key has a non-empty passphrase, eclipse will not be able to load it, even if you provide the correct password. 
  4. Now, just save it in the same location you loaded it from, confirm empty passphrase, overwrite it, apply and click ok.
Pushing your project to your github repo

Final step is to push your project to your remote repository on github. 
  1. Finally, select the project you want to push to your created repository, right click it, go to Team -> Share Project... and select git.
  2. Select your project, click on Create Repository and Finish. Now you should be able to commit and update changes to your local git repository
  3. To push it to your remote github repo, right click your project, select Team -> Remote -> Push... 
  4. Fill the URI with your project SSH address, which is shown when you enter your repository page on github.
  5. Select ssh protocol, user is git and empty password.
  6. Cross your fingers, hit next. If it doesn't work for some reason at this point, try restarting Eclipse (it worked for me after restart).
  7. Select Source Ref and Destination Ref (basically the branch), click on Add Spec and Finish on the next screen.
  8. Hopefully everything goes well.  
Importing a project from a github repo

Easier than pushing is checking out a project.
  1. From Eclipse, select File -> Import and select Projects from Git under Git.
  2. Select Clone and fill this form just like step 4 of the previous section.
  3. Select the branch you would like to check out.
  4. Select the location you would like to checkout to.
  5. You should be back to Import Projects from Git window with your newly cloned repo showing.
  6. Select your repository and follow the instructions (defaults should be fine).
Adding a passphrase to your ssh key

After configuring eclipse, you should probably set a non-empty passphrase to your ssh key. It is not the scope of this post to discuss why you shouldn't leave your passphrase empty, but here is how:
$ cd ~/.ssh
$ ssh-keygen -f id_rsa -p
This command will prompt for a new passphrase and you are done!

Hope this was helpful. Feel free to comment or add any info.

[1] http://help.github.com/linux-key-setup/
[2] http://help.github.com/troubleshooting-ssh/
[3] http://blog.doityourselfandroid.com/2010/08/14/git-github-and-egit-eclipse-integration/
[4] http://www.eclipse.org/egit/

Cheers,
Lucas Piva

2 comments:

  1. Thanks for a great tutorial, got me up and running in no time.

    ReplyDelete
  2. Thanks for your helpful tutorial. May I know why Eclipse + EGit don't accept passphrase during remote->push? is it a bug?

    ReplyDelete