How to connect Eclipse to GitHub


Link to my new GitHub: https://github.com/ssodelta

I spent the better part of the day today trying to get Eclipse connected to GitHub and I finally got it to work, so I thought I’d share my findings to make it easier for other newbies.

First of all, a lightning round of how Git works. In Git you have a repository, which is basically the directory containing your files. Drawing a parallel to Eclipse, the source directory is similar to the Git repository. There are two types of repositories you have to be acquainted with, there are local repositories and remote repositories. The local repositories are the repositories located locally on your hard disk, and the remote repositories are stored in the cloud, for instance GitHub.

There are a few commands you can use, but these are the most important ones:

  • Pull – Here you sort of pull the data from the remote repository to your local one, i.e. download the data.
  • Commit – Save your changes in the local repository.
  • Push – Here you update your files in the remote repository with the files from your local. That is, you upload them.

To just use GitHub as a remote repository, these are all the commands you’ll need for now. If you’re interested in more, I might make a future post on it, or else Google’s your friend.

Installing EGit for Eclipse

Now, let’s get started. The first thing you’ll need is to install EGit for Eclipse. Go to Help > Install New Software… in Eclipse and you should see the following dialog:

dialog1

Here you’re going to click Add… and you’ll see this next dialog:

dialog2

The first input box is just the name of the plugin, so write EGit or something similar. And for the Location we need the URL of the update path of the plugin, which is http://download.eclipse.org/egit/updates. So your dialog should look like:

dialog22

Click OK to return to the original dialog and check both available check boxes as such:

dialog3

Now just click Next and let Eclipse do its thing.

Setting up your remote repository

First create an account on GitHub. Then if you go to your profile, you should see something like:

dialog4

Then click on Repositories and click the green New button:

dialog5

Then name your repository and etc. But make sure to check the box that says Initialize this repository with a README as such:

dialog6

If you don’t do this step, you’ll need to run a command from command-line, so it’s easier to get GitHub to do it for us.

Setting up SSH keys

In order to make a secure connection between your computer and GitHub’s server you need to matching SSH-keys. So first of all, we’re going to generate a key in Eclipse. Go to Window > Preferences > General > Network Connections > SSH2 and you should see:

dialog7

Make sure the textfield called Private keys has an entry called id_rsa. Next click on Key Management to see:

dialog8

Next click on Generate RSA Key and copy the contents of the large text box to the clipboard – you’ll need it later.

snap

Now fill in a secure passphrase and hit Save Private Key… to C:\Users\*your-username*\.ssh as id_rsa with no file extension. Now go to your GitHub project and go to Deploy Keys:

snap2

Now click on Add deploy key:

snap3

Fill in your public key (the one you copy-pasted) into the Key text field and give it a memorable name (such as My Home PC). The name doesn’t really matter, but you might as well name it something easy to remember.Hit Add key and go back to Eclipse.

Connecting Eclipse to GitHub

Now that we’ve set up the keys we actually want to connect your project to GitHub.

Find your project in the workspace. Right click on it, and go Team > Share Project… You should see this menu:

snap6

Choose Git and click Next 

You should see this menu:

snap7

You’re going to click Create… here, which should reveal:

snap8

Name your project whatever you want and hit Finish twice (on the previous menu as well) and let Eclipse do its thing.

Now go to Window > Open Perspective… > Git

snap4

You should see your Git project in the explorer:

snap9

Click the little arrow to the left to see:

snap10

Right click on Remotes and choose Create Remote… which should reveal:

snap11

Your Remote Name might be called origin, but call it master and click OK. We’re going to configure Push now.

snap12

Hit Change… to see:

snap121

Fill in the textbox as mine here, except you need to replace /user/ with your username (SSODelta in my case) and projectname.git as your project name on GitHub. To make this easier, take a look at the URL of your GitHub project:

snap13

Just add .git to this, remove the https://github.com/-part and add it to the popup menu. Now click Finish.

Now, under Ref Mappings click Add… to see:

snap16

Start writing (just type an m) in Remote branch and Eclipse should give you a suggestion. Just choose that and click OK. You should now see something like:

snap14

Now go back to your project in the Git explorer to see:

snap17

Under master right click on the entry with a green arrow pointing left and choose Configure Fetch… Follow the same procedure as before and you should see something like:

snap15

Fetching and Pushing your Project to GitHub from Eclipse

Go back to the Java perspective and find your project. Right click on it, choose Team > Commit… and choose Commit Push and you should see it working! If not, write me message and I’ll try to assist you – otherwise Google is your friend.

Good luck!

Advertisements

15 thoughts on “How to connect Eclipse to GitHub

  1. Saw your entry in the feeds and was thinking “I swear if this guy posts code in pastebin again, I’m unfollowing him”. Hehe. Good job.

    1. Finally got around to connecting it! I’ve actually always wanted to use GitHub, but found it too confusing to use, so I used pastebin instead.

  2. I am getting this error while clicking “Configure fetching”(green)arrow on above screenshot on.
    “No ref to fetch from BDE – origin – everything up to date.”

  3. Hi,
    I have recently upgraded my computer and then downloaded eclipse mars but I can’t find team>share there where you are showing. can you please let me know what can I do?
    thanks

  4. Hi
    I am getting error while trying to push:git+ssh://git@github.com/smartBuy/test5.git: push not permitted
    The key was added with push option

    can you help?

    1. The error occurs because you don’t have authorization to push unto the given branch. Are you sure you have added the correct RSA key?

      Otherwise it’s perfectly possible to do this without RSA, just use https:// instead and login using your GitHub credentials.

  5. Hi, I am trying this on Eclipse Kepler..
    SO i completed all the steps with the RSA.
    When I got to any project in my eclipse and right click and select “Team”,
    The share project is not selectable(its not enabled) only apply patch is selectable.

    How to continue.

Comment on this article

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s