1

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?

jsmdnq
  • 363
  • 2
  • 10
  • 1
    Does this help? http://www.vogella.com/articles/EGit/article.html – bryanmac Dec 01 '12 at 00:34
  • @bryanmac Thats what I used to do it in the first place. But when I followed those instructions it moved(not copied) my project to the git repository(the eclipse workspace dir was basically empty). I then imported a project from the git repository to get it back. Maybe I missed a step or didn't click something I was suppose to in the process to get it to clone automatically for me? – jsmdnq Dec 01 '12 at 00:41
  • This is what I'd like to do too, did you ever work out a solution? – stu Mar 18 '14 at 18:07

2 Answers2

1

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.

0

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • But I do not want the the repository in my project path... or at least, I do not want the main one there. I want to pretty much have exactly the same setup as if I cloned from a remote repository(which is basically what I have to do) so that there is the "master" version and then the cloned version. This way I can keep a history. If I do it your way and, say, my eclipse project dir becomes corrupt then I can lose everything. My way, the eclipse git is really just a clone of a branch. – jsmdnq Dec 01 '12 at 12:15
  • For example: `D:\Git\MyEclipseProjects` <== main location of "head" of projects. `C:\Eclipse\Workspace\MyEclipseProjects` <== cloned project used in eclipse(a "local" copy). When I make changes to the eclipse project it gets saved in `C:\Eclipse\Workspace\MyEclipseProjects`. When I commit the changes update `D:\Git\MyEclipseProjects`(which could also be a remote site). This is how I do it now, and I think it is better(safer) but it's a lot work to set it up like this. Maybe I need to make a script to automate it? – jsmdnq Dec 01 '12 at 12:19
  • @jsmdnq my point is: your git repo is always in your Eclipse project repo. So import the onle you want (the one from your Eclipse workspace, or the one outside). If you want to add a remote repo pointing to another local clone, you can too. – VonC Dec 01 '12 at 12:35
  • Yes, but I do not want that. Again, what if the Eclipse project directory gets corrupted or deleted or whatever? As I said a few times, I already have a master repository dir on my HD that I want to use for all my clones. When I want to backup all my projects I can just back up that drive. BUT I do not want eclipse using it as a location to store projects either(it doesn't use it as a project directory but EGit pulls all the files from it instead of the eclipse workspace). – jsmdnq Dec 01 '12 at 13:00
  • Remember, I want two copies that are not at the same place(one a clone or branch and the other the master). – jsmdnq Dec 01 '12 at 13:01
  • @jsmdnq that reminds me of http://stackoverflow.com/questions/7259167/how-to-keep-git-folder-out-of-a-cloned-eclipse-project or http://dev.eclipse.org/mhonarc/lists/egit-dev/msg02828.html: your `.git` is always in your Eclipse project. If you want security, make one Eclipse project a git clone of the other, add it as a remote an commit/push regularly to your remote (ie your other local clone Eclipse project that you are using for backup) – VonC Dec 01 '12 at 13:33