Actions

EmSys

EGit - Git version control with Eclipse

From EdWiki

Git with Eclipse (EGit)

This tutorial describes the usage of EGit; an Eclipse plug-in to use the distributed version control system Git. This tutorial is based on Eclipse 4.3 (Kepler).

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.

Install Git

We need to install command line version of git before installing EGit.

Installation of Git into Eclipse

EGit Install

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


Setting up Git in Eclipse

Git and Eclipse configuration

EGit Configuration

Eclipse uses the same configuration files as the Git command line tools hence if you have configured your Git setup via Eclipse or via the command line, both will be using the same setup.

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. These values are stored in the same way the Git command line would store them, so that you can use EGit and Git for the same Git repository.

Default Repository Location

EGit Configuration

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.

Setup rules for ignoring files and directories

Git can be configured to ignore certain files and directories. This is configured in a .gitignore file. Egit also supports the .gitignore file for excluding files and directories from the Git operations.

Activating the Git Menu

Activating Git Menu

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

Repository Type

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.


Config Git Repo

On the next dialog press the Create button.


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.


Create Git Repo

Enter your project name as Name for your local Git repository. Select the Finish button.

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.

Using the Git Staging view

Create Git Repo

Eclipse gives you several options to stage and commit your changes. The Git Staging view provides a convenient compact overview on all changes you have done since checking out a branch.

Open the Git Staging view via the Window → Show View → Other... → Git → Git Staging menu.

In this view you select all files which have changed and drag them into the Staged Changes area. To commit the staged changes you write a descriptive commit message and press the Commit button which is highlighted in the screenshot.

Perform these actions for your initial changes. Afterwards the first version of your project is under version control. Your data is now safely stored in your local Git repository and you can always restore your Eclipse project to this initial point.

Using the Git Staging view for committing changes

The Git Staging view is a very convenient way of working with Git as it gives you a grouped view of all the pending changes without an additional dialog.

Now, you can create new file(s), modify the existing files. Then, using the Git Staging view drag files (which you want to commit) into the Staged Changes area, write a meaningful commit message and press the commit button.

If you prefer to invoke the Git commit dialog directly you can do this via selecting the Team → Commit dialog. The dialog allows you to add changed and new files to the staging area and commit the changes.

Repository view

EGit Toolbar Repo

EGit has a Git repository view which allow you to browse your repositories, clone Git repositories, checkout projects, manage your branches and much more.

Open the Git Repositories view via the Window > Show Views > Other ... Select Git repositories menu.

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.


Integration with GITLAB

  1. It is assumed that you are familiar with GITLAB, If not, Please get familiarise with GITLAB.
  2. It is necessary to create a project of the same name on the GITLAB#Create Project on the git Server before you do any push, fetch operation in Eclipse.

Setup the private key on Eclipse

eclipse SSH2
  1. Navigate to Window > Preferences
  2. Go to General > Network Connections > SSH2, go to the General tab
  3. Click the Add Private Key button
  4. Browse for the private key you’ve saved under /home/usrname/.ssh

This setting is depicted in the following screenshot.



Create Remote

EGit Repositories

Go to Window > Show Views > Other ... Select Git repositories. Expand the repository to see Remotes, right click and Create Remote




EGit New Remote

Choose one of the option : Configure push/Configure fetch and click OK

push will tell eclipse you're allowed to write (which is the correct option to push on that repo). then name that remote repository like you want (the first is usually named "origin", but you can have "prod", "test-server", ...)

fetch will tell eclipse you're only allowed to read (which is the correct option if you don't want/have the right to push on that repo).

Click OK to Configure push for remote origin.

Configure remote for Push

Configure remote for Push

In this dialog click on the Change... button.









Select a URI

Specify the URI of the repository fields as follows:

Host = 10.114.15.15
Repository Path = usrname/projectname.git

and in the Authentication tab

User = git
Password =                 # leave it blank

Clicking Finish button will takes you back to Configure Push Dialog.

Select a URI


We have configured the remote orgin URI, next we have to define Refspecs (how local branches or tags are mapped to branches or tags in a remote repository). To define Refspecs, click on the Add... button.







Push Refspec

A typical example for a Push RefSpec could be

HEAD:refs/heads/master

This means that the currently checked out branch (as signified by the HEAD Reference) will be pushed into the master branch of the remote repository.

Click OK followd by Save and Push.

If every thing goes-well, You will see a screen some-thing similar to the following;

Push Result









See Also

  1. Getting Started with GITLAB
  2. Working with Remote Repositories
  3. GIT Basic Commands