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:
Here you’re going to click Add… and you’ll see this next dialog:
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:
Click OK to return to the original dialog and check both available check boxes as such:
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:
Then click on Repositories and click the green New button:
Then name your repository and etc. But make sure to check the box that says Initialize this repository with a README as such:
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:
Make sure the textfield called Private keys has an entry called id_rsa. Next click on Key Management to see:
Next click on Generate RSA Key and copy the contents of the large text box to the clipboard – you’ll need it later.
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:
Now click on Add deploy key:
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:
Choose Git and click Next >
You should see this menu:
You’re going to click Create… here, which should reveal:
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
You should see your Git project in the explorer:
Click the little arrow to the left to see:
Right click on Remotes and choose Create Remote… which should reveal:
Your Remote Name might be called origin, but call it master and click OK. We’re going to configure Push now.
Hit Change… to see:
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:
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:
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:
Now go back to your project in the Git explorer to see:
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:
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.