Java, Eclipse and Web programming Tutorials
Follow me on twitter About Lars Vogel

Git with Eclipse (EGit) - Tutorial

Lars Vogel

Version 1.5

02.03.2010

Revision History
Revision 0.111.12.2009Lars Vogel
Splitted from http://www.vogella.de/articles/Git/article.html
Revision 0.212.12.2009Lars Vogel
Chapter on bugs
Revision 0.316.12.2009Lars Vogel
Getting EGit
Revision 0.422.12.2009Lars Vogel
Added bug for http via proxy checkout
Revision 0.503.01.2010Lars Vogel
Added info for HTTP checkout
Revision 0.626.01.2010Lars Vogel
New update site for EGit
Revision 0.706.02.2010Lars Vogel
Github
Revision 0.810.02.2010Lars Vogel
how to make team operations availabe
Revision 0.911.02.2010Lars Vogel
how to get Egit via Egit
Revision 1.012.02.2010Lars Vogel
How to use GitHub with EGit
Revision 1.115.02.2010Lars Vogel
Updated due to fixed bug 298027
Revision 1.216.02.2010Lars Vogel
Added team operations
Revision 1.317.02.2010Lars Vogel
Change order of section
Revision 1.429.02.2010Lars Vogel
added link to multiple repository description from Ekke
Revision 1.502.03.2010Lars Vogel
Added .gitignore, improved Github description, Mylyn connector

Using Git with Eclipse with EGit

This article describes the usage of EGit; a Eclipse plugin to use the distributed version control system Git.


Table of Contents

1. Overview
2. EGit Installation
3. Issues with EGit
4. Putting projects under version control
4.1. Put a new project under version control
4.2. Checkout existing project
4.3. Create a Git repostitory for multiple projects
5. Team operations
6. Using EGit with Github
6.1. Create Repository in Github
6.2. Create project in Eclipse
6.3. Push project to Github
6.4. Mylyn integration with Github
7. Hacking EGit - Getting the source code
8. Thank you
9. Questions and Discussion
10. Links and Literature

1. Overview

Git is a distributed version control system written in C. For an introduction into Git and the usage of the command line for Git please see Git Tutorial . JGit is a library which implements Git in Java. EGit is an Eclipse Team provider for Git (using JGit).

This article will describe the usage of EGit.

2. EGit Installation

Install the EGit Eclipse plugin from http://download.eclipse.org/egit/updates . Please see Eclipse Update manager for information on how to use the update manager.

3. Issues with EGit

Currently EGit and JGit are in an early phase of their development. Here are the issue which are critical for me.

In addition to these a few usuabilty enhancements would be nice.

4. Putting projects under version control

The following explains how to create a repository for one project and shows how to checkout an exiting projects from a remote repository. The next chapter will demonstrate how to use EGit.

4.1. Put a new project under version control

Create a project "de.vogella.git.first". Create the following class.

				
package de.vogella.git.first;

public class GitTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Git is fun");
	}
}

			

Right click your project, select Team -> Share -> Git

Select the proposed line and press "Create repository". Press finish.

You have created a local Git repository.

Create the file ".gitignore" in your project with the following content. All files / directories which apply to the pattern described in this file will be ignored by git. In this example all files in the "bin" directory will be ignored.

				
bin

			

4.2. Checkout existing project

EGit allow to clone an existing project. We will use an Eclipse plugin as example. To learn more about accessing standard Eclipse coding please see Eclipse Source Code Guide .

Select File -> Import -> Git -> Git Repository.

Import for example the following project "git://dev.eclipse.org/org.eclipse.jface/org.eclipse.jface.snippets.git". You only have to paste the URL to the first line of the dialog, the rest will be filled out automatically.

After pressing next the system will allow you to import the existing branches. You should select at least "master".

The next dialog allow you to specify where the project should be copied to and which branch should be initially selected.

You get an additional import dialog.

You have checked to the project and can use the team Git operation on your project.

4.3. Create a Git repostitory for multiple projects

The following blog entry describes how to create several projects in one git repository: http://ekkescorner.wordpress.com/blog-series/git-mercurial/workflow-create-multi-project-repositories-egit/

5. Team operations

Once you have placed a project under version control you can start using team operations on your project. The team operations are available via right mouse click on your project. You can:

  • Select "Team" -> "Add to version control" on the project node to add all files to version control.

  • Select Team -> Commit to commit your changes to the local Git repository.

  • Select Team -> Branch to create and to switch between branches.

6. Using EGit with Github

6.1. Create Repository in Github

Github is a popular hosting provider for Git projects. To use Github go to their webpage and create an account. Public trees are hosted by Github for free.

If you do not yet have an account sign-up now for Github. During the sign-up you will be asked to provide a "passphase". This "passphase" is later needed to push to Github from your local repository.

Create a new repository. Call it "de.vogella.git.github"

Github is nice enough to tell you what to do if you work on the command line. Do not follow the instructions.

				
Global setup:

 Download and install Git
  git config --global user.name "Lars Vogel"
  git config --global user.email youremail...
        

Next steps:

  mkdir de.vogella.git.github
  cd de.vogella.git.github
  git init
  touch README
  git add README
  git commit -m 'first commit'
  git remote add origin git@github.com:vogella/de.vogella.git.github.git
  git push origin master
      

Existing Git Repo?

  cd existing_git_repo
  git remote add origin git@github.com:vogella/de.vogella.git.github.git
  git push origin master
      

Importing a Subversion Repo?

  Click here
      

When you're done:

  Continue


			

6.2. Create project in Eclipse

Create a new project in Eclipse with the name "de.vogella.git.github". Create a new local repository for this project and commit to this local repository.

6.3. Push project to Github

Right-mouse click on your project and select "Team" -> "Push to". Select the proposed line and press "Create repository" and afterwards select "Finish". Maintain the following data. Adjust the first line so that you are using your user and your project name.

				
git+ssh://git@github.com/vogella/de.vogella.git.github
			

Maintain your passphase which you maintained during the Github setup.

Select your branch (you should currently only have master if you followed my tutorial), press "Add all branches spec" and select next.

Press finish.

If you now select your github Dashboard and then your project you should see the commited files.

6.4. Mylyn integration with Github

Eclipse Mylyn is a productively tool for programmers. David Green and others is working on a github Mylyn connector. Please see Github Mylyn Connector Wiki .

7. Hacking EGit - Getting the source code

EGit is self-hosted on git://egit.eclipse.org . You can clone the EGit code from the repository using EGit using the following URL git://egit.eclipse.org/jgit.git and git://egit.eclipse.org/egit.git .

You also need some libraries from Orbit. See Libraries from Orbit for getting these libraries.

8. Thank you

Thank you for practicing with this tutorial.

Please note that I maintain this website in my private time. If you like the information I'm providing please help me by donating.

9. Questions and Discussion

For questions and discussion around this article please use the www.vogella.de Google Group. Also if you note an error in this article please post the error and if possible the correction to the Group.

I believe the following is a very good guideline for asking questions in general and also for the Google group How To Ask Questions The Smart Way.

10. Links and Literature

http://wiki.eclipse.org/EGit/User_Guide EGit User Guide

http://wiki.eclipse.org/EGit/Contributor_Guide EGit contributor guide

Git in 5 Minutes by Scott Paul Robertson

http://progit.org/ Free Git book

https://dev.eclipse.org/mailman/listinfo/egit-dev eGit Mailing list

http://www.ibm.com/developerworks/opensource/library/l-git-subversion-1/index.html Git Intro for subversion users by Teodor Zlatanov

http://alblue.blogspot.com/2010/02/git-for-eclipse-users.html Git for Eclipse users by Alex Blewitt