Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Git version control with Eclipse Tutorial

Git with Eclipse (EGit)


This tutorial describes the usage of EGit; an Eclipse plug-in to use the distributed version
control system Git.

NOTE: This description contains information about working with Git in Eclipse, but it does not
cover all concepts for Git. To learn about git, please see Getting Started with GITLAB

What is EGit?
EGit is an Eclipse plug-in (software component) which allows you to use the distributed
version control system Git directly within the Eclipse IDE. EGit is based on the JGit library.
JGit is a library which implements the Git functionality in Java.

Installation of Git into Eclipse


The EGit plug-in can be installed into every Eclipse IDE installation.

Most Eclipse 4.2 and Eclipse 4.3 downloads from eclipse.org contain EGit in their default
configuration. In this case no additional installation is required.

If the EGit plug-in is missing in your Eclipse installation, you can install it via the Eclipse
installation manager. Start this manager via the Help → Install new Software... menu entry.

Copy and paste the following URL in the dialog.

http://download.eclipse.org/egit/updates
If you do not see “Eclipse Git Team Provider” in the list of software, you probably have it
installed already. Continue to next step.

Setting up Git in Eclipse


Before using Git you must configure your name and email address which is used to fill the
author and committer information of commits you create.

The Git configuration settings can be adjusted via the Eclipse preference setting.

Select Window → Preferences → Team → Git → Configuration to see the current


configuration and to change it.

You can add entries to your Git configuration by pressing the Add Entry... button on the Git
Configuration preference page. To add your user, use the user.name as key and your real
name as value. Repeat the procedure for your email address.

You can add more values in this dialog.


Default Repository Location
You can also configure in Eclipse the default folder for storing Git repositories.

Select Window → Preferences → Team → Git → Default Repository Folder entry.

Note:You can also use Eclipse configuration variables to define this path, e.g. if you want to
store repositories in the folder "git" under the Eclipse workspace you may use
${workspace_loc}/git.
To simplify access to the common Git operations you can activate the Git menubar. For this
select Window → Customize perspective... and check the Command groups Git and Git
Navigation Actions in the Command Groups Availability tab.

Working with a local Git repository in Eclipse


One major advantage of Git compared to SVN or CVS is that you can easily create local
repositories, even before you share them with other people. In this way, you can version your
work locally. This allows you to keep track of your changes in the project and allows you to
revert to another state at a later point in time.

Creating a local Git repository


To put your new project under version control with Git, right-click on your project, select Team
→ Share Project

Depending on your installation you may have to select that you want to use Git as a version
control system.
It is recommended to separate your Git repository from any additional meta-data which
Eclipse might create, it is recommended to place your Git repositories outside the Eclipse
workspace. Eclipse follows this recommendation and the EGit plug-in proposes a directory
outside your workspace.

Caution: Placing Git repositories directly in the workspace may cause performance issues
since the Git support in Eclipse then may need to scan a large number of files reachable
under the workspace.

Select repository type Git and click Next.

The first time we create a repository we will create a general directory to hold all of the
repositories for our projects. Click on "Create" and then type in a parent directory name and
click "Finish".
After pressing the Finish button, the wizard displays the settings for your local Git repository.
Select the Finish button again to put your repository under Git version control.

You have created a local Git repository. The Git repository is in this case directly stored in the
specified folder in a .git folder.

• Note the decorator text "[…….. NO-HEAD]" behind the project name. This label
means that there is no branch (not even a master yet) for the project until we make
our first add and commit, the No-Head text will appear. Note also the question mark
decorator by files (*.java). This shows that the Java file is not yet under version
control.

Adding files to repository to track changes

To add files to our Git repository, we right-click Team > Add to Index on the project
name. The + decorator show that the project's files have been added to version control.
Git Repositories view
Eclipse Git has a Git repositories view which allow you to browse your repositories, add or
initialize local repositories or clone remote repositories, checkout projects, manage your
branches and much more.
The toolbar entries allow you to add an existing local Git repository to the view, clone a Git
repository and to create a new Git repository.

Committing the Changes to the repository


When you do a Git commit you are essentially saving a snapshot of your project -- a recording
of what the files in your project look like at that point. For example, here is an illustration of a
series of commits over a period of time, with files being added.
Think about Git as a tool for storing, comparing, and merging snapshots of your project. To
make our first commit for our project:
• Click Team > Commit in the project context menu
• Enter a commit message explaining your change, the first line (followed by an empty
line) will become the short log for this commit. By default the author and committer
are taken from the .gitconfig file in your home director
• Click Commit to commit your first change.

Check-off 1:
Edit your project and make a Commit. Reapet it and make a second commit.
Right-click on the Project name and choose Team > Show in History.
Copy the history records of your commit to your report.

To compare the code from our two commits, we first click the Compare Mode toggle

button in the History View. Then, we click (select) the master commit in the history list
and then double-click on source Java file in the resource list (bottom right panel in History
View). A new window will appear outlining the differences (changes) between the two
committals of code.
Check-off 2:
Copy the changes window and include it in your report.

Eclipse and Github


GitHub is an online; browser based distributed version control system for software
developers using the Git revision control system. The service provides free public
repositories, issue tracking, graphs, code review, downloads, wikis, collaborator
management, and more. There are also social networking elements of the service
including feeds, the ability to follow other users, and network related graphs. In order
to use its facilities you have to create an account.
Step 1: Create Account
Follow the instruction for creating an account un the following link:
http://www.wikihow.com/Create-an-Account-on-GitHub
Push Wizard
Enables to check-in your repository to Github
Team > Remote > Push...
Push URI

• If you already configured a Push Specification in the Repositories View you


may also select it here using the drop-down list under Configured remote
repositories. The Finish button will be enabled if the Push Specification for
this remote is configured properly (i.e. has at least one URI and a ref spec.
• Otherwise click Custom URI and enter the URI of the upstream repository you
want to push to.

ssh://git@github.com/
is the URI of GitHub.
The rest is the name of
your directory for the
repository.

Enter your details as


were defined in your
account.

Push Ref Specifications


See also Refspecs for more explanations.
Click Next
If this is the first time you connect to this repository via ssh you will have to accept
the host key of the remote repository
If your ssh key is protected by a passphrase (which is recommended) you have to
enter it here

Click Add all branches spec


This is a convenient way to declare that you want to map your local branch names to
the same branch names on the upstream repository you want to push changes to.
Click Add all tags spec to map local tags 1:1 to tags in the repository you want to
push to.
If you want to map local branches to those in the upstream repository in a different
way you may define more detailed mapping specifications in the following way

• enter source and destination ref or select already existing branches from the
drop-down lists
• click Add Spec

This will transfer the newly defined mapping to the list Specifications for push

You might also like