jsmdnq
jsmdnq

Reputation: 363

How to properly use EGit with Eclipse

The only way I seem to be able to get use git in eclipse is to create a non-git project then turn that into a repository. This ends up moving the project out of the eclipse workspace on the file system. I then have to delete the project(it's still in the git repository), then import a git repository after creating a branch and clone it to get it back into the eclipse workspace.

Is there a simpler way?

I simply want to create project that is really a clone from a local repository. Essentially I have two copies on my HD but I can commit the eclipse project to the git repository. Unfortunately there has to be a better way?

Upvotes: 1

Views: 3691

Answers (2)

Adam Finucane
Adam Finucane

Reputation: 36

To answer this question involves three steps.

  1. Create a external (remote) repository
  2. Share the project in a way that it remains in the workspace
  3. Connect the project repository to the remote repository

Create a Remote Repository on Your Local Machine

Switch to the Git perspective. Click on Create a new Git Repository.

When a dialog appears select a directory where you would like your remote repository to be. This is were your project will be pushed to. You will also want to select the Create as a bare repository option.

create bare repository

That's it for this step. You should now have this repository in your EGit repositories list.

Sharing Project within the Workspace

There are two ways to share a project so that it remains in your workspace. 1. Make the project directory a repository 2. Make the whole workspace directory a repository

The Project as a Repository

The first option option is not recommended by the Eclipse team. This issue is described in more detail at Why is not recommended to have an Eclipse project folder as a Git repository?.

The basics of the issue are twofold:

  1. You can't have more than one project per repository
  2. If some thing happens to your workspace you'll lose your repository too

Issue 1 isn't solved here. Issue 2 can be solved by connecting to a remote repository as show later.

To share the project as a repository:

  1. Right click on the project
  2. Select Team -> Share Project... from the popup menu
  3. Click Use or create repository in parent folder of project parent folder repository
  4. Select the project from the list
  5. Click on the Create Repository button
  6. Click the Finish button

Next you'll want to connect your newly created repository to the remote repository. That's covered below.

The Workspace as Repository

The second options allows multiple projects to be added to your repository. In fact any new project you create will automatically added to the repository.

Automatically adding projects can cause some issues.

One issue is that, if there are changes in multiple projects, staging those changes can take a bit of wading through. Using a Tree presentation when staging can simplify things.

Another more serious issue occurs when importing a git clone of a project into the workspace. This importing will create nested repositories. Nested repositories can cause problems according to this post. By default Eclipse doesn't import the git clone of the project into the workspace.

To share the workspace as a repository:

  1. Right click on the project
  2. Select Team -> Share Project... from the popup menu
  3. Click on the Create button
  4. Select your workspace as your Repository directory
  5. Click the Finish button

Once your repository is created you may want to do a little house keeping. I suggest adding the RemoteSystemsTempFiles project to the .gitignore file. Note: the .metadata file is added automatically by Eclipse.

You can ignore the RemoteSystemsTempFiles by:

  1. Switching to the EGit perspecitive
  2. Selecting the workspace repository from the list of repositories
  3. Select the Git Staging tab
  4. Click on the View Menu button on the right side the the tabs toolbar
  5. Select Presentation -> Tree menu (folders are easier to ignore form the tree view) tree presentation
  6. Right click on the RemoteSystemsTempFiles project
  7. Select the Ignore Folder menu
  8. Ignore other projects in the same way

Connecting to the Remote Repository

The last stage is connecting the workspace repository to the remote repository we created earlier. Once you've switched to the EGit perspective:

  1. Expand your workspace respository
  2. Right click on the Remote node in the tree
  3. Select the Create remote... menu
  4. Leave the remote name as origin
  5. Select Configure fetch
  6. Press Ok
  7. Click on the Change... button
  8. Click on the Local File button
  9. Select the bare repository that you created in the first section
  10. Click Finish
  11. Click Save and Fetch and then Ok enter image description here

You can then stage, commit and push changes in your projects and workspace.

When you first commit and push EGit will push the default branch master to the remote and configure pulling this branch from remote repository.

Upvotes: 1

VonC
VonC

Reputation: 1323125

Follwing the User Guide, you would need to create the .git repo within your current project path:

select project

git path

If the .git path is within the current project path, there is no reason EGit moves your files anywhere else.

Upvotes: 0

Related Questions