ChatGPT解决这个技术问题 Extra ChatGPT

Importing a GitHub project into Eclipse

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 ?

https://i.stack.imgur.com/Hjc8R.png

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 ?

https://i.stack.imgur.com/4pxtY.png

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.
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.

C
Community

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

Eclipse for GitHub

EGit 3.x manual (section "Starting from existing Git Repositories")

Eclipse with GitHub

EGit tutorial

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

File → Import → Git → Projects from Git

https://i.stack.imgur.com/MWw0j.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.

https://i.stack.imgur.com/gvM1X.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.


"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 ?
@user470184: no, an Eclipse workspace can be and should be in an independent place from the projects themselves.
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."
@user470184: following stackoverflow.com/questions/251116/… and stackoverflow.com/questions/6396349/…, 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.
@user470184: See 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)
A
Alain Beauvois

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

File -> Import -> Git -> Projects From Git > URI Enter the Github repository url Select the branch


And what if no projects are found?
Works neat for me..Simple and easy to remember.
it imports the projects into Eclipse, but it's not a Java project, still struggling, please help if you can. The OP described exactly the situation I'm facing now.
I always get a TransportError…
b
bomb zj

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

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

Ecplise Version: Mars.2 Release (4.5.2)


this is pretty simple and works fine for me unless I don't intend to use different location for my project. Thanks!
you mean Git perspective?
yes, import projects from it using New Project wizard.
C
Community

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 and change it to org.eclipse.jdt.core.javanature 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>

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
Changing .project wasn't enough for me. I also had to right-click on project > Properties > Project Facets > Convert to faceted form
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.
problem persists as of 2016/01. Came here again to read my own answer. I'm so awesome.
G
Glorfindel

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


M
M.Usman

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.


N
Nate Sun

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.


c
chmodsss

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