Algorithms and OOD (CSC 207 2013F) : Labs

Getting Started with Git (in Eclipse)


Summary: In this laboratory, you will practice using Git. Although you can use the command line (see commands below) to manage your GitHub repositories, we will try communicating with GitHub directly from Eclipse, using the EGit plugin. After all, if we're using an IDE, we should integrate everything, including our source code management system.

If you do though opt to go the command line route in the future, you can find a list of useful commands at the end of this lab.

Preparation

a. Make sure you have read the Introduction to Git reading.

b. Make sure that you have done the Introduction to Eclipse lab

c. Log in and open a terminal window.

d. Start Eclipse.

Exercises

Exercise 1: Create a GitHub Account

Create a GitHub account at https://github.com/

Exercise 2: Configure Your Linux Account

As you may recall, your life is easier if you do a bit of configuration before working with Git. You'll find it useful to configure in both the terminal and Eclipse. We'll start by working in the terminal.

First, open a new terminal window.

Next, type these commands.

$ git config --global user.name "YourName"
$ git config --global user.email username@grinnell.edu

Finally, set your editor.

$ git config --global core.editor editor

You can use emacs, vi (or vim, or vim.basic), or even gedit.

Exercise 3: Add EGit

EGit may not be installed on your workstation, so you get to configure Eclipse to use EGit.

a. From the Help menu, select Install New Software....

b. In the dialog box that pops up, click Add.

c. In the next dialog box that pops up, use "EGit" as the name and http://download.eclipse.org/egit/updates as the location.

d. You should then see Eclipse Git Team Provider and JGit in the middle pane. Select them both.

e. Click the Next > button.

f. You'll probably get an error message and a longer list of items. Uncheck Task focused interface for Eclipse Git Team Provider (thanks to StackOverflow for helping me figure this out).

g. Click the Next > button.

h. Eclipse will ask you to review the items to be installed. Click the Next > button.

i. Review the user license.

j. Click the Finish button.

k. After the software installs, you will probably need to restart Eclipse.

Exercise 4: Create a Workspace

Create a folder on your computer where you will store your GitHub repositories. For example, you might name it CSC207.

Exercise 5: Configuring Eclipse to Use Your New Directory

Go into Eclipse Window > Preferences. A preferences window should open. Click the triangle next to Team. Then, click on Git. Change the default repository to the one you just created and click Apply.

Exercise 6: Configuring Git

You should still be in the preferences window. If not, reopen it. Then click the triangle next to Git to expand that item. Click on Configurations and make sure that under user it says something like the following.

    email    youremail@grinnell.edu

    name     Your Name

If it doesn't you should add new entries for both those values. Click on Add Entry and enter “user.name” as the Key and your username on GitHub as the Value. Click on Add Entry again and enter “user.email” as the Key and the email you used for GitHub as the Value.

Exercise 7: Connecting Eclipse to GitHub

To help identify yourself, you will need a pair of RSA keys.

Once again, open the Preferences window if it's not already open.

a. In Eclipse, expand the General preference. Then expand Network Connections. Select SSH2 Click the Key Management Tab then Generate RSA Key. Copy the public key to your clipboard. Enter a passphrase. Click Save Private Key to save your private key and click Apply. Exit the preferences.

b. Now we have to tell GitHub about the key. Log in to your account on the GitHub Web site. Go to the your Account Settings (which may appear as a pair of tools). Under SSH Keys click Add SSH key. Give it a descriptive name (EGit), and paste the public key you just copied from Eclipse.

Exercise 8: Creating a New Repository

a. Log in to your GitHub account.

b. Somewhere on the page (along the right hand column, at the time of this writing, but it changes), there should be a button labeled New Repository (although that name changes, too). Click that button. A dialog should appear.

c. Name your repository (e.g., git-exercise). Click the buttons to make it public and to initialize the repository with a README file. Choose the Java .gitignore file. Choose a license you like. Finally, click Create Repository.

d. Copy the SSH link associated with your repository. You will need to look for the Clone URL button (currently along the right-hand side of the page), click on SSH, and then click on Copy to Clipboard.

The link should look something like the following:

git@github.com:username/sample-java.git

Exercise 9: Getting the Repository into Eclipse

Warning! These steps are long, tedious, and exacting. However, they will become second nature after awhile.

Switch back to Eclipse.

  • Select File > Import....
  • Expand the Git item.
  • Double-click on Projects from Git.
  • Double-click on URI.
  • Paste the URI you just copied from GitHub.
  • Confirm that the Host is “GitHub.com” and that the Repository path is something reasonable.
  • Under Authentication set the User to “git”.
  • Click the Next button.
  • Make sure that master is checked.
  • Click the Next button.
  • Verify that the folder selected is the one you created a few exercises ago (perhaps with a subfolder added for this project).
  • Leave the Initial branch as “master” and the Remote name as “origin”.
  • Click the Next button.
  • We're now ready to turn the repository into a project. Click the Use New Project Wizard. Then click the Finish button.
  • Click Java Project and then the Next button.
  • We'll need to use our Git directory as the location for the project. Untick Use default location. Then click Browse and select your cloned repository.
  • Enter a project name. To avoid confusion, you may want to give it the same name as your repository. (Eclipse may have done this for you already.)
  • Click Finish.
  • Take a deep breath. You've finished linking Eclipse to your Git repository.

You an now add, delete, and modify files as you please. The next challenge will be committing and pushing them back to the main repository.

Exercise 10: Getting Changes to Appear on GitHub

Making our repository an Eclipse project introduced some changes. For example, there's now a hidden .project file and the .gitignore file has expanded a bit. We should commit these changes to the repository.

Right click on your new project in the Package Explorer. Select Team > Commit. A window will appear. We can select the modified and added files (.gitignore, .classpath, and .project), enter a message (e.g., “Add Eclipse project files”) and click Commit.

This commit remains local. To get it to the main repository, we right click on the project and then select Team > Push to Upstream.

Why do this now? So that we can distinguish these changes from our own.

Exercise 11: Add Some Java

In the Eclipse lab, you created a simple “Hello World” Java program. Create a similar program in your new repository. That is,

a. Create a new package, such as edu.grinnell.csc207.username.hello.

b. Create a new class, such as HelloWorld.

c. Add a main method to the class that has the following form.

    public static void main(String[] args) {
        System.out.println("Hello, GitHub!");
    } // main(String[])

d. Run your program to make sure that it's correct.

e. Save your program.

Exercise 12: Commit and Push Your Code

We've updated the repository and so should commit the code to the repository. We'll also push it back to the main repository. But instead of the two-step process, try using the Commit and Push button.

Note that we usually don't push immediately immediately after each commit, but it's handy to do so now. Normally, we do a series of small commits and only push once we've reached a larger goal.

Go to GitHub and see if the changes you have made are visible. You may need to navigate a few layers deep, since Eclipse makes a directory for each portion of the package.

Exercise 13: Making Updates on GitHub

Although we normally update code in our local copy of the repository, it is also possible to update code directly on GitHub. Navigate to the Java code you just wrote and click the Edit button.

Change the output. Then scroll to the bottom of the page, enter a commit message, and click Commit.

Exercise 14: Pulling Updates from GitHub

Our primary repository is now updated. How about our local repository? Let's see. Switch back to Eclipse. Open the Java code you created and edited. Has it changed?

It is unlikely that the Java code changed. Why? Because you haven't told Eclipse to pull the updated version. That's a two step process.

First, right click on the project and select Team > Fetch from Upstream. Next, right click on the project and select Team > Merge. Cross your fingers that everything worked.

Exercise 15: Conflicting Changes

On GitHub, update the Java file by adding another print statement.

In Eclipse, update the Java file by changing the output.

Determine what happens when you try to fetch and merge.

For Those With Extra Time

Extra 1: Other Local Modifications

Find out what Eclipse does if you modify one of the files in the Git repository using Emacs or an editor of your choice.

Extra 2: Experimenting with Forks

You'll need to do this exercise with a partner.

Create a fork of your partner's repository.

Figure out how to copy your fork into Eclipse.

Extra 3: Shared Repositories

You'll need to do this exercise with a partner.

First, figure out how to give someone else administrative access to your repository.

Next, verify that they have administrative access by having them make a change and upload it to the repository.

Finally, figure out what happens if the two of you make changes.

Some Useful Git Commands

git config --global user.name "Your Name"
git config --global user.email your_email@example.com
git config --global core.editor editor

git help
git clone
git add
git commit
git pull
git push

Copyright (c) 2013 Samuel A. Rebelsky.

Creative Commons License

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.