Git With Eclipse (EGit) - Tutorial
Git With Eclipse (EGit) - Tutorial
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
317
by Lars Vogel
12.12.2009 - 13.01.2013
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.2 (Juno).
Table of Contents
1. What is EGit 2. Command line Git 3. EGit Installation 4. Setting up EGit 5. Using EGit for a local Git repository 5.1. Overview 5.2. Creating an Eclipse project 5.3. Creating a local Git repository 5.4. Create .gitignore file 5.5. Initial commit 5.6. Making changes and commiting them 5.7. Commmiting files directly 5.8. Show changes 6. Commit messages 6.1. Importance of Git commit messages 6.2. Guidelines for useful commit messages 6.3. Example 7. Repository view 8. Clone existing project 9. Using EGit 9.1. Basic operations 9.2. Merge 9.3. Solving Merge Conflicts 9.4. Git amend 9.5. View the resource history 10. Branching 10.1. What are branches? 10.2. Switch Branches 11. Create Patches 12. Blame annotations 13. Stash support for uncommited changes 13.1. Stash to save uncommitted changes 13.2. Stash via the Git repository view 14. Git repository for multiple projects 14.1. Create a new repository 14.2. Add a project to an existing Git repository 15. Tutorial: Create Git repository for multiple projects 16. Using EGit with Github
BACK TO TOP
1 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
16.3. Push project to Github 16.4. Clone repository from Github 17. Mylyn integration with Github 18. Hacking EGit - Getting the source code 19. Thank you 20. Questions and Discussion 21. Links and Literature 21.1. EGit and Git Resources 21.2. vogella Resources
1. 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.
3. EGit Installation
EGit can be installed into every Eclipse IDE installation. Most Eclipse 4.2 packages from Eclipse.org contain EGit in there default configuration. In this case no additional installation is required. If EGit is missing in your Eclipse installation, you can install it via the Eclipse Update Manager via: Help Install new Software. EGit can be installed from the following URL.
http://download.eclipse.org/egit/updates
BACK TO TOP
2 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
4. Setting up EGit
EGit allow you to configure your default user and email address for each commit. To setup your user and email address select Window Preferences Team Git Configuration EGit will automatically read your default .gitconfig in which your global Git settings are stored. You can add entries via pressing the Add Entries button. To add your user, use the user.name as key and your real name as value. Repeat the procedure via your email address.
BACK TO TOP
3 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
You can add more values in this dialog. These values are also valid for the Git command line in case you are also using the command line.
To put your new project under version control with Git, right click on your project, select Team Share Project Git.
4 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
EGit will propose an directory outside your workspace. It is recommended to place your Git repositories outside the Eclipse workspace to avoid that you add unwanted data created by the Eclipse IDE. Enter your project name as Name for your local Git repository. Select the Finish button.
BACK TO TOP
5 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
After pressing Finish the The wizard shows you the settings for your local Git repository. Select the Finish button 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. The following screenshot shows the directory structure .
BACK TO TOP
6 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
All files and directories which apply to the pattern described in this file will be ignored by Git. In this example all files in the bin and the .metadata directory will be ignored. You can also use EGit to add files and folder to a local .gitignore via right-mouse click on the file or folder and by selecting Team Ignore but the Git convension is to have one .gitignore in the root directly which describes the files which should get ignored.
BACK TO TOP
7 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
Write a descriptive commmit message and press the Commit button which is hightlighted in the following screenshot.
Now the first version of your Java project is under version control. If you don't experience any hardware error you data is now savely stored in your Git repository and you can always restore your Eclipse project to this initial point.
package de.vogella.git.first;
8 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
Also create a new file called readme.txt We want to commit the changes of GitTest class but not add and commit the readme.txt file to the Git repository. Using the Git Staging View drag only the GitTest class into the Staged Changes area, write a good commit message and press the commit button.
BACK TO TOP
9 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
Via right mouse click on an individual file you can compare this file with its ancestor (the commit before that) or with the current version in the workspace.
6. Commit messages
6.1. Importance of Git commit messages
The commit message describes the changes done by a commit and is used as the first point of checking the history of a Git repository. A commit message should therefore be descriptive and informative without repeating the code changes.
6.3. Example
The following can serve as an example for a commit message.
Short summary (less than 50 characters) Detailed explanation, if required, line break at around 72 characters more stuff to describe...
7. Repository view
EGit has a Git repository view which allow you to browse your repositories, checkout projects and manage your branches.
BACK TO TOP
10 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
BACK TO TOP
11 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
Press clone and enter the URL to your Git repository. Git supports several protocols, e.g. git:// and https://. You only have to paste the URL to the first line of the dialog, the rest will be filled out automatically. Please note that some proxy servers block the git:// protocol. If you face issues, please try to use the https:// protocol. For example the following URI can be used to clone the example projects of the Eclipse 4 application development book: [email protected]:vogella/eclipse4book.git
BACK TO TOP
12 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
After pressing next the system will allow you to import the existing branches. You should select at least master as this is typically the main development branch.
The next dialog allows you to specify where the project should be copied to and which branch should be initially selected.
BACK TO TOP
13 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
After the Git repository is cloned, EGit opens an additional import dialog which allows to import the Eclipse projects from the Git repository.
Once this dialog is completed, you have checked out (cloned) the projects into a local Git repository and you can use Git operation on these projects.
BACK TO TOP
14 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
9. Using EGit
9.1. Basic 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, 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" -> "Push" to push your change to a remote Git repository (see the GitHub chapter). "Team" -> "Tag" allows you to create and manage tags.
9.2. Merge
EGit supports merging of branches to add the changes of one branch into another if this branch has not been changed. Select your project and Team Merge to start the merge dialog.
You can manually edit the text on the left side or use the Copy current change from right to left button to copy the conflicting changes from right to left.
BACK TO TOP
15 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
Once you have manually merged the changes, select Team Add from the context menu of the resource to mark the conflicts as resolved and commit the merge resolution via Team Commit.
BACK TO TOP
16 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
10. Branching
10.1. What are branches?
Git allows you to create branches, i.e. independent copies of the source code which can be changed independently from each other. The default branch is called master. Git allows you to create branches very fast and cheaply in terms of resource consumption. Developers are encouraged to use branches frequently.
BACK TO TOP
17 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
This file can be used to get applied to another Git repository, via Team Apply Patch
BACK TO TOP
http://www.vogella.com/articles/EGit/article.html
vogella.com Tutorials Training Services Publications Connect this, right click on your file and select Team Show Annotations. Afterwards you can place the mouse on the left side of the editor and a popup will show the commit information.
BACK TO TOP
19 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
BACK TO TOP
20 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
EGit moves the projects to the repository and imports the project automatically into your workspace.
BACK TO TOP
21 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
Create at least on Java class in each project. Git is not able to save empty folders. Afterwards select both projects, right click on them and select Team Share Git.
BACK TO TOP
22 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
BACK TO TOP
23 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
After creation of your new repository Github tells you what to do if you would inport via the command line. As we are going to use EGit we can ignore this information.
Maintain your passphase which you maintained during the Github setup.
BACK TO TOP
24 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
Select your branch (you should currently only have master if you followed my tutorial), press "Add all branches spec" and select next.
Press finish.
BACK TO TOP
If you now select your github Dashboard and then your project you should see the commited files.
25 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
26 of 27
1/21/2013 11:28 AM
http://www.vogella.com/articles/EGit/article.html
vogella.com
Tutorials
Training
Services
Publications
Connect
http://wiki.eclipse.org/EGit/User_Guide EGit User Guide http://wiki.eclipse.org/EGit/Contributor_Guide EGit contributor guide Git Tutorial
BACK TO TOP
27 of 27
1/21/2013 11:28 AM