- Install and configure EGit within Eclipse, if not already done.
- Create a project in Eclipse.
- Add project to local git repository.
- Create a project in GitHub (with the same name as our local project).
- Check in our project to the remote git repository.
- The two phases of commitment.
- Helpful Resources.
Prerequisites are as below.
- You have an account already on GitHub. Free accounts can be used to create only public repositories i.e. everybody will be able to see your code. So if you need a private Git repository on GitHub, you have to pay.
Here you will install the EGit plugin within Eclipse. This gives Eclipse the ability to interact with Git repositories. If you have Eclipse Kepler (4.3), Eclipse Luna (4.4) or later, then this easily installed through the Eclipse Marketplace.
- Open Eclipse, go Help → Eclipse Marketplace.
- In the search field, type
EGitand press ENTER.
- The first result will be EGit - Git Team Provider 3.5.0 (latest version as of this writing).
- Click the Install button.
- Select Eclipse Git Team provider component.
- Select Next, accept license agreements, let it install, let Eclipse restart.
Before you can use EGit, you need to configure it.
Set up your identity. In Eclipse, go Window → Preferences → Team → Git → Configuration. Enter your name and email - Git will use this to identify who is making commits.
Set up location for local repositories. In Eclipse, go Window → Preferences → Team → Git and set a location for Default Repository Folder. I have used
It is a good idea to create your Git repositories in a different directory than your Eclipse Workspace. The Git repository folders will hold lots of files related to Git setup, not just your project files. If you create your Git repository within your Eclipse Workspace, some operations in Eclipse would suffer a performance hit because they will have to scan all of these other files, not just project files.
You will need to create this directory first and it should be empty.
In Eclipse, select File → New → Project → select whatever project type you want (I am using a Java Project). Give it a name and press Finish. The contents of the project aren't important - it is just a few bare files to show the next part of the process - adding it to a local repository and then a remote one.
Note that your project has been created in your Eclipse workspace. In my case, this was
In Package Explorer view, right click on the project you just created and select Team → Share Project. In the
Share Project dialog, select Git and then click Next.
The next screen is
Configure Git Repository.
I am not selecting
Use or create repository in parent folder of project for reasons stated above - the repository folder will contain a lot of Git-specific files apart from my project files and I do not want Eclipse having to scan these folders.
The general idea is that you have one project per repository, so we make one repo for this project. Click Create.
Give the new repository the same name as your project and click Finish.
I am now back on the
Configure Git Repository dialog.
I can see the following:
D:\Documents\Work\Git\TestProject\.git- all the files related to Git go here.
- Working Directory:
D:\Documents\Work\Git\TestProject- the parent folder for the Git directory and the (new, target) project directory.
- Current Location:
D:\Documents\Work\JavaTests\TestProject- where the project directory current is, but it will be moved to Target.
- Target Location:
D:\Documents\Work\Git\TestProject\TestProject- new location for the project files.
Note that the Working directory field (read only) gives the repository location (
D:\Documents\Work\Git in my case). Also note that the Current Location and Target Location are different. Current Location shows where my files are right now - in my Eclipse workspace. Target Location shows where my files will be moved to - a folder underneath the Git repository Working directory.
This means that when you press Finish, Eclipse will move your files out of the Eclipse workspace and into the Git repository folder. Don't be surprised - this is normal. It takes some getting used to if you haven't used Git before. In the Git repository, you will also notice a new folder -
.git, which contains all the files Git uses to manage the repository.
Now take some time to notice what has changed. The icons in Package Explorer are different.
Note the duplication in names - I have a Git repository called
TestProject containing a project called
TestProject. The icons reflect Git state. See EGit/User Guide/State which has this image showing at a glance the different states.
Also note that your files have indeed been moved out of your Eclipse workspace and into your Git repository.
Open up the Git Staging view: Window → Show View → Other → select Git → Git Staging.
This view makes it very easy to commit changes.
You can also do this with the commit action. Control+shift+3 is the default keyboard shortcut, or right click on the project and select Team → commit. However, I find the Git Staging view much easier to use.
Go to GitHub and log in.
On the actions menu, select New repository.
Give Repository name the same value as your Eclipse project's name, and whatever Description is appropriate. Note that my repository is Public, so that anyone will be able to see what I check into it. Click Create Repository.
This screen gives us very important information that we will use in the next step - the HTTP URL for interacting with this repository from EGit in Eclipse: https://github.com/robertmarkbram/TestProject.git.
Back in Eclipse, right click on your project in Package Explorer and select Team → Remote → Push...
Most importantly, in the URI field, enter the URL we got from GitHub earlier. The Host and Repository path fields will auto-fill correctly. In the Authentication section, enter your GitHub username and password. I let Eclipse store these details by selecting Store in Secure Store. If this is the first time I have done it, Eclipse will ask me to set up a
Click Next and you will see the
Push to: ... dialog.
Click Add All Branches Spec. Under Source ref select master [branch]. Under Destination ref, select refs/heads/master.
Click Next and see the Push Confirmation dialog. Click Finish.
The project files will then be pushed to the GitHub remote repository. When it is complete, you will see a results dialog.
If you refresh your GitHub repository, you should now see the files committed remotely.
And underneath the main project folder are my files.
We know have a system of two phase commits, so to speak. Work locally and commit changes to the local repository so you have a history and backups as needed.
When you commit, you have a choice of using Commit or Commit and Push. Just choosing
Commit will commit your changes to the local repository. Selecting
Commit and Push will commit them to your local repository and then push them to the remote repository - e.g. GitHub. The first time you use
Commit and Push, it will ask you to set up the remote repository.
Press next and you will see the
Push Branch master dialog wherein you tell Git what to do when pulling changes from the upstream repository i.e. bring changes from GitHub back into your local.
Hit Next and the rest of this is like any other commit, but you will have sent your changes to two repositories - local and GitHub.
- Git version control with Eclipse (EGit) - Tutorial by Lars Vogel.
- EGit Tutorial by Jonas Helming.
- GitHub Help - such as the Create A Repo page.
- This Git book.
- This brilliant Git Cheatsheet.