Technical miscellanea by Derrick Coetzee

Using Eclipse’s EGit with GitHub

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.

Plugin installation:

  1. Open Eclipse. Make sure it is either installed in your home directory (user directory on Windows) or that you are running as root/administrator.
  2. 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.
  3. 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.
  4. Open up the items provided and check “Eclipse EGit”, “EGit Project Set Support”, and “Eclipse JGit”. Don’t check the remaining items.
  5. Click Next. Continue clicking Next until the install is complete, then Finish.
  6. Close Eclipse and re-open it as your normal user.

Setting up keys (must be done only once per user)

  1. Go to Window->Preferences. Select General->Network connections->SSH2.
  2. 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.
  3. 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.
  4. 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

  1. Go to Github. Click your username in the upper-right. Click New repository.
  2. Fill out the fields and click “Create repository”. You will be given an information screen. You’ll need to refer back to this screen.
  3. Go to Eclipse. Create/open the project.
  4. Right-click the project in the Package Explorer pane and select Team->Share Project. Select Git. Click Next.
  5. 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”.
  6. 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).
  7. 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.
  8. 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 git@github.com:username/Blah.git” and copy the part after “origin”. Put it in the “URI” field.
  9. For Protocol select SSH. Change nothing else. Click Next. If you are asked to accept Github’s host key, click Yes.
  10. For Source ref select from the dropdown “master [branch]“. Destination ref will fill in with the same. Click “Add all branches spec”. Click Finish.
  11. Refresh the information screen on Github to verify your check-in completed successfully.

Importing an existing GitHub repository

  1. Choose File->Import. Select Git->Projects from Git. Click Next.
  2. Choose URI. Click Next.
  3. 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.
  4. Enter the URI in Eclipse. Select SSH for protocol. Click Next.
  5. Select the branches you want (usually only one, master, will be offered and will be automatically selected). Click Next.
  6. Select where the local files and git metadata will be stored. The default should be fine. Click Next.
  7. If it is an Eclipse project previously added using EGit, choose Import existing project. Otherwise choose one of the other options.
  8. 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.

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>