128

I've imported a GitHub project but how do I setup the project in my Eclipse project workspace?

Do I need to set the GitHub working directory(c:\users) to my Eclipse working directory(c:\Projects) prior to checking out the project ?

enter image description here

Edited :

My working directory is now c:\users\projectname\.git So then I try to import the project using the eclipse "import" option. When I try to import selecting the option "Use the new projects wizard" the source code is not imported, if I import selecting the option "Import as general project" the source code is imported but the created project created by Eclipse is not a java project. When selecting the option "Use the new projects wizard" and creating a new java project using the wizard should'nt the code be automatically imported ?

enter image description here

blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • 7
    Having roughly the same issue. I don't see how it was answered below. Yes, the Egit user manual describes how to clone the repository. The egit user manual also describes steps which is apparently causing the behavior you describe (and in my case, "Use the New Projects wizard" doesn't import the source either). I'm confused how the steps provided in egit produce an empty project and this is "normal behavior". So where is the "normal behavior" that creates a project with source from a cloned local repository? I tried to synchronize my empty project with the repository but doesn't work. – s_t_e_v_e Feb 03 '13 at 20:19
  • 2
    Frustrating thing is that I found the source "checked out" (I don't know if that's the correct git term) on the filesystem and don't recall what I did to get it there. At one point I browsed into the local repository branches and selected checkout. (Didn't think anything happened and I couldn't find any cancel or undo check out.) Regardless once the source is there, its easy enough to create a new java project and point it to that directory. – s_t_e_v_e Feb 03 '13 at 20:37

8 Answers8

109

As mentioned in Alain Beauvois's answer, and now (Q4 2013) better explained in

Copy the URL from GitHub and select in Eclipse from the menu the

File → Import → Git → Projects from Git

http://wiki.eclipse.org/images/5/5a/Egit-0.9-import-projects-select-repository.png


If the Git repo isn't cloned yet:

In> order to checkout a remote project, you will have to clone its repository first.
Open the Eclipse Import wizard (e.g. File => Import), select Git => Projects from Git and click Next.
Select “URI” and click Next.
Now you will have to enter the repository’s location and connection data. Entering the URI will automatically fill some fields. Complete any other required fields and hit Next. If you use GitHub, you can copy the URI from the web page.

Select all branches you wish to clone and hit Next again.

Hit the Clone… button to open another wizard for cloning Git repositories.

http://eclipsesource.com/blogs/wp-content/uploads/2012/12/14-282x300.png


Original answer (July 2011)

First, if your "Working Directory" is C:\Users, that is odd, since it would mean you have cloned the GitHub repo directly within C:\Users (i.e. you have a .git directory in C:\Users)

Usually, you would clone a GitHub repo in "any directory of your choice\theGitHubRepoName".

As described in the EGit user Manual page:

In any case (unless you create a "bare" Repository, but that's not discussed here), the new Repository is essentially a folder on the local hard disk which contains the "working directory" and the metadata folder.
The metadata folder is a dedicated child folder named ".git" and often referred to as ".git-folder". It contains the actual repository (i.e. the Commits, the References, the logs and such).

The metadata folder is totally transparent to the Git client, while the working directory is used to expose the currently checked out Repository content as files for tools and editors.

Typically, if these files are to be used in Eclipse, they must be imported into the Eclipse workspace in one way or another. In order to do so, the easiest way would be to check in .project files from which the "Import Existing Projects" wizard can create the projects easily. Thus in most cases, the structure of a Repository containing Eclipse projects would look similar to something like this:

See also the Using EGit with Github section.


My working directory is now c:\users\projectname\.git

You should have the content of that repo checked out in c:\users\projectname (in other words, you should have more than just the .git).

So then I try to import the project using the eclipse "import" option.
When I try to import selecting the option "Use the new projects wizard", the source code is not imported.

That is normal.

If I import selecting the option "Import as general project" the source code is imported but the created project created by Eclipse is not a java project.

Again normal.

When selecting the option "Use the new projects wizard" and creating a new java project using the wizard should'nt the code be automatically imported ?

No, that would only create an empty project.
If that project is created in c:\users\projectname, you can then declare the eisting source directory in that project.
Since it is defined in the same working directory than the Git repo, that project should then appear as "versioned".

You could also use the "Import existing project" option, if your GitHub repo had versioned the .project and .classpath file, but that may not be the case here.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 3
    "If that project is created in c:\users\projectname, you can then declare the eisting source directory in that project." Does that not mean that the Eclipse workspace dir must be the same as the Git working dir ? – blue-sky Jul 20 '11 at 20:23
  • 2
    @user470184: no, an Eclipse workspace can be and should be in an independent place from the projects themselves. – VonC Jul 20 '11 at 20:45
  • 2
    Ok, so how do I link an Eclipse java project to the Git repository ? Can you elaborate on "declare the eisting source directory in that project." – blue-sky Jul 20 '11 at 21:16
  • 2
    @user470184: following http://stackoverflow.com/questions/251116/where-in-an-eclipse-workspace-is-the-list-of-projects-stored/251129#251129 and http://stackoverflow.com/questions/6396349/eclipse-egit-clone-project-into-workspace/6396416#6396416, the idea is to have or create the `.project` and `.classpath` in the same parent directory than the one of the `.git`, and then edit its property to reference the source directory. The reference of that project will live in the Eclipse workspace. But the actual working directory for that project will be where the `.git` is. – VonC Jul 20 '11 at 21:40
  • @user470184: See http://www.vogella.de/articles/Eclipse/article.html#firstjava: when creating a new project, you can specify what source directory it needs to consider. If you create that project on the same directory than the one including the `.git`, and if you configure it to reference the existing source directory coming out of the cloned GitHub repo, you have a project which will be seen by Eclipse as versioned (because of the presence of the `.git`) – VonC Jul 20 '11 at 21:46
  • Thanks for your patience with this. "If you create that project on the same directory than the one including the .git" How can I create the project in same directory as one where .git is stored. The .git is stored in a seperate location(c:\users\admin\gitstore) to the workspace (c:\workspace). – blue-sky Jul 20 '11 at 22:22
  • @user470184: When you create a new project, you can specify where to create it. By default, the path is the one of the Eclipse workspace. But you can select a "custom" path and specify the parent directory of the `.git`. The `.project` and `.classpath` will be created there even though the project will be registered in the workspace (as explained in http://stackoverflow.com/questions/251116/where-in-an-eclipse-workspace-is-the-list-of-projects-stored/251129#251129) – VonC Jul 21 '11 at 03:53
  • I now have my project created but im unable to compare or commit files to the Git repo. Should I be ab le to perform compare/commit directly from the project package explorer ? – blue-sky Jul 21 '11 at 09:18
  • @user470184 let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1693/discussion-between-vonc-and-user470184) or in http://chat.stackoverflow.com/rooms/1204/vonc – VonC Jul 21 '11 at 10:36
  • I've tried to import javascript as a Javascript project and the source code is still missing. This should be reported as a bug. Even if there is no src dir declared Eclipse still should allow you to explore the directory tree cloned from the repository. That is how it works when you import from CVS and from SVN. Don't see why this can't be the default for git. – Cheruvim May 22 '14 at 14:19
  • @Cheruvim Do you have any error message? That would be a good question to ask here, with the version of Eclipse, Egit, and git that you are using. – VonC May 22 '14 at 14:57
  • Wow. This actually worked for a Java project. (I used the "auto" search for projects.) Eclipse should do a usability refactoring to make this import workflow simple as I expect many people are using Git/GitHub. I know *anything* can be done with Eclipse, but it's becoming a tool for the "grayhairs" as young people use other IDEs rather than tolerate Eclipse's ten steps to do something that others do in two steps. – Lance Kind Aug 28 '20 at 21:00
22

With the last ADT, you can import Github project using Eclipse :

  1. File -> Import -> Git -> Projects From Git > URI

  2. Enter the Github repository url

  3. Select the branch

Alain Beauvois
  • 5,896
  • 3
  • 44
  • 26
9

I encountered the same problem, and finally found a way:

  1. Add or create git repository in git view.
  2. Click new java project menu, uncheck "use default location", select the git repo location, click finish.

Ecplise Version: Mars.2 Release (4.5.2)

bomb zj
  • 91
  • 1
  • 3
8

unanswered core problem persists:

My working directory is now c:\users\projectname.git So then I try to import the project using the eclipse "import" option. When I try to import selecting the option "Use the new projects wizard" the source code is not imported, if I import selecting the option "Import as general project" the source code is imported but the created project created by Eclipse is not a java project. When selecting the option "Use the new projects wizard" and creating a new java project using the wizard should'nt the code be automatically imported ?

Yes it should.

It's a bug. Reported here.

Here is a workaround:

  • Import as general project

  • Notice the imported data is no valid Eclipse project (no build path available)

  • Open the .project xml file in the project folder in Eclipse. If you can't see this file, see How can I get Eclipse to show .* files?.

  • Go to source tab

  • Search for <natures></natures> and change it to <natures><nature>org.eclipse.jdt.core.javanature</nature></natures> and save the file

    (idea comes from here)

  • Right click the src folder, go to Build Path... and click Use as Source Folder

After that, you should be able to run & debug the project, and also use team actions via right-click in the package explorer.

If you still have troubles running the project (something like "main class not found"), make sure the <buildSpec> inside the .project file is set (as described here):

<buildSpec>
    <buildCommand>
        <name>org.eclipse.jdt.core.javabuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
</buildSpec>
Community
  • 1
  • 1
phil294
  • 10,038
  • 8
  • 65
  • 98
  • thanks for your answer but I have given up on egit after a myriad of issues. I'm just using git bash, it's simple, always works and not ideal dependant. Would have saved myself many hours if I'd moved sooner – blue-sky Sep 04 '15 at 22:16
  • Changing .project wasn't enough for me. I also had to right-click on project > Properties > Project Facets > Convert to faceted form – xtian Sep 29 '15 at 09:49
  • What do you mean you're 'just using git bash'? For the life of me, I just can't import a github/bitbucket project into Eclipse in ANY way at all. I clone the project into a local repository and paste the files into an eclipse project manually. – user2651804 Oct 02 '15 at 12:10
  • problem persists as of 2016/01. Came here again to read my own answer. I'm so awesome. – phil294 Jan 11 '16 at 13:17
4

Using the command line is an option, and would remove the need for an Eclipse Plugin. First, create a directory to hold the project.

mkdir myGitRepo
cd myGitRepo

Clone the desired repository in the directory you just created.

git clone https://github.com/JonasHelming/gitTutorial.git

Then open Eclipse and select the directory you created (myGitRepo) as the Eclipse Workspace.

Don't worry that the Project Explorer is empty, Eclipse can't recognize the source files yet.

Lastly, create a new Java project with the exact same name as the project you pulled. In this case, it was 'gitTutorial'.

File -> New -> Java Project

At this point, the project's sub directories should contain the files pulled from Github. Take a look at the following post in my blog for a more detailed explanation.

http://brianredd.com/application/pull-java-project-from-github

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Brian Redd
  • 414
  • 1
  • 5
  • 12
0

It can be done in two ways:

1.Use clone Git

2.You can set it up manually by rearranging folders given in it. make a two separate folder 'src' and 'res' and place appropriate classes and xml file given by library. and then import project from eclipse and make it as library, that's it.

M.Usman
  • 2,049
  • 22
  • 25
0

I think you need to create a branch before you can import into your local Eclipse, otherwise, there is an error leading to incapable of importing repository from Github or Bitbucket.

Nate Sun
  • 11
  • 3
-1

When the local git projects are cloned in eclipse and are viewable in git perspective but not in package explorer (workspace), the following steps worked for me:

  • Select the repository in git perspective
  • Right click and select import projects
chmodsss
  • 711
  • 8
  • 18