This is an old revision of the document!


Using Git with Eclipse

The Eclipse plugin that integrate Git with Eclipse is called EGit.

Installation

EGit should have been installed when you installed Eclipse. If not, you can install it using any of the methods described on the EGit downloads page .

Configuration

To configure Git, click on Window+Preferences. Then, expand Git and click on Configuration.

Since many Git servers use your email address for authentication, it is important that the user.email value exists and is correct.

You can edit an existing entry by clicking on it. You can add an entry by clicking on Add Entry… and then entering a key and value. Keys are hierarchical, with levels delimited by a . character (e.g., user.email).

Opening the Git Perspective

Eclipse enables you to present and interact with the same project in a variety of different ways, each of which is called a “perspective”. To enable the Git perspective, click on Window+Perspective+OpenPerspective and select Git (if it is available) or Other…+Git.

Creating a Local Repository

The easiest way to create a local repository is to:

  1. Click on “Create a new local Git repository”

Note that you should not put the repository under the directory containing the Eclipse workspace.

Adding a Project to Version Control

A project should be added to version control only once. After that, files in the project can be added to version control individually (or in groups). It is best to create a local repository for the easiest to add a project to version control

To add a project to version control:

  1. Create a local repository (if you have not already done so)
  2. Right-click on the project
  3. Pull down to Team+Share
  4. If asked to choose a code management system, click on Git
  5. Browse to the local repository to use (or create a local repository in the parent folder of the project)
  6. Click on Finish

The name of the local repository you are using for this project will now appear next to the name of the project. In additions, the icons on any directories/files in the project will change to indicate that they have not yet been committed.

If you look at the project directory (outside of Eclipse) you will see that it is now empty, and if you look at the repository directory (again, outside of Eclipse) you will see that it now contains all of the directories and files that were in the project' directory.

Ignoring Files

The easiest way to instruct Git to ignore files is to:

  1. Click on the “Git Staging” tab
  2. Right-click on the file/directory
  3. Pull down to Ignore

You can also edit the .gitignore file (outside of Eclipse).

Committing Files

The easiest way to instruct Git to commit files to the local repository is:

  1. Click on the “Git Staging” tab
  2. Select the appropriate files in the “Unstaged Changes” list
  3. Click on +
  4. Enter a descriptive commit message
  5. Click on Commit

Pushing a Repository for the First Time

The first time you push a local repository, you will be asked to provide several pieces of information.

For most common Git work flows, the “Remote name” should be origin (indicating that this is the location from which everyone pulls).

The “URI”, “Host”, “Protocol”, and “Port” will vary with the Git host.

You will also be asked to provide a “User” and “Password” for that host. If you select “Store in Secure Store”, Eclipse will save this information for you locally, and you will not need to provide it each time you push. Otherwise, you will need to authenticate yourself each time you push.

Cloning a Repository

The easiest way to clone a remote repository is to:

  1. Click on “Clone a Git repository”
  2. Enter the “URI”, “Host”, “Repository path”, and “Protocol”
  3. Enter your authentication information for the Git server
  4. Select the appropriate branch (probably master) and click on Next
  5. Enter the appropriate local directory for the “Destination”, the appropriate “Remote name” (probably origin), and click Finish.

At this point, you will have cloned the repository but you will not have any Eclipse projects. To import a project from the repository, while still in the Git Perspective you should:

  1. Expand the repository
  2. Expand the “Working Tree”
  3. Right-click on the project of interest
  4. Select Import Projects
  5. Click on Finish

Summary of Common Git Commands

Most Git documentation assumes that you are issuing git commands within a shell. The following table summarizes how to execute these commands using Egit.

git Command EGit Equivalent
add Right-click on the file and then Team+Add to Index
clone Open the Git Perspective and click on
commit Right-click on the project/files and then Team+Commit…
pull Right-click on the project and then Team+Pull
push Right-click on the project and then Team+Push

To add an entry to the .gitignore file, right-click on the file and then Team+Ignore.