Although most people seem to rely on the command-line git client, I recently tried to get Eclipse integration for Git going in order to help someone out who was not so command-line savvy. But I want them to also be able to store their public repositories on GitHub easily. So I figured out how to go about this on Ubuntu 11.10, using the standard version of Eclipse, currently version 3.7.0, and installed version 1.3.0 of EGit. I relied on the Getting Started and Pushing a Local Project to GitHub parts of the EGit user guide.
- Open Eclipse. Make sure it is either installed in your home directory (user directory on Windows) or that you are running as root/administrator.
- I read claims that EGit comes with version 3.7.0, but it didn’t come with mine. To see if yours has it, go to Help->About and choose “Installation Details”. If you see “Eclipse EGit”, it is already installed.
- Go to Help->Install new software. Enter the EGit plugin update URL http://download.eclipse.org/egit/updates and hit “Add…” Enter EGit for the name. Click OK.
- Open up the items provided and check “Eclipse EGit”, “EGit Project Set Support”, and “Eclipse JGit”. Don’t check the remaining items.
- Click Next. Continue clicking Next until the install is complete, then Finish.
- Close Eclipse and re-open it as your normal user.
Setting up keys (must be done only once per user)
- Go to Window->Preferences. Select General->Network connections->SSH2.
- If you have already generated a key pair for use with Github, skip to step 4. Otherwise, go to the Key management tab and select Generate RSA Key. Then click Save Private Key to save the private key file, which you will use in step 3. Copy the public key. If you wish to use an existing key pair you generated in the past, instead click Load Existing Key, then copy the public key.
- Go to Github, log in, click your username in the upper-right, click the “Edit your profile” button, click SSH keys, click Add SSH key, and paste in the public key from step 2.
- Go back to the Eclipse SSH2 preferences. Select the General tab. Click Browse and select the directory where your private key is stored. Enter its filename in the “Private keys” field.
Putting a new/existing project into a new GitHub repository
- Go to Github. Click your username in the upper-right. Click New repository.
- Fill out the fields and click “Create repository”. You will be given an information screen. You’ll need to refer back to this screen.
- Go to Eclipse. Create/open the project.
- Right-click the project in the Package Explorer pane and select Team->Share Project. Select Git. Click Next.
- Click “Create”. Enter a name (the same name as your GitHub repository is fine). Click OK. Click Finish. Your project should now say next to it “NO-HEAD”.
- Right-click your project, choose Team->Commit. The name should be the name you wish your check-ins to be attributed to, and the e-mail should match the one given on Github’s information page from step 2 (after user.email).
- Check all the files you want to commit – you should usually include at least any source files, “.classpath”, and “.project”. Enter a commit message. Click OK.
- Right-click your project, choose Team->Remote->Push. On Github’s information page from step 2, find the line that looks like “git remote add origin firstname.lastname@example.org:username/Blah.git” and copy the part after “origin”. Put it in the “URI” field.
- For Protocol select SSH. Change nothing else. Click Next. If you are asked to accept Github’s host key, click Yes.
- For Source ref select from the dropdown “master [branch]“. Destination ref will fill in with the same. Click “Add all branches spec”. Click Finish.
- Refresh the information screen on Github to verify your check-in completed successfully.
Importing an existing GitHub repository
- Choose File->Import. Select Git->Projects from Git. Click Next.
- Choose URI. Click Next.
- On the GitHub project’s page, click “SSH” (selected by default), then copy the full URI (starting with git@…). Make sure it says “Read+Write access” to the right of it.
- Enter the URI in Eclipse. Select SSH for protocol. Click Next.
- Select the branches you want (usually only one, master, will be offered and will be automatically selected). Click Next.
- Select where the local files and git metadata will be stored. The default should be fine. Click Next.
- If it is an Eclipse project previously added using EGit, choose Import existing project. Otherwise choose one of the other options.
- Click Next. Click Finish.
Once you’ve gotten your project set up, try right-clicking on source files and messing around with the options under “Team” to see what they do. “Show in history” is particularly useful, opening the history view which allows you to explore previous versions of files. If you do Team->Commit, it should automatically select previously committed files that have changes, but new files have to be checked to add them.
You can use the “Compare with” menu to view changes to your file over time; “Compare with HEAD revision” shows changes since your last commit. Compare views dynamically update as you make modifications.