How can I create a project in the Git repository root using Eclipse EGit? - eclipse

I would like to add the project files of a brand-new Eclipse project to the root-level of a git repo. I would like it to look like something like this:
./projectrepo/.git
./projectrepo/.project
./projectrepo/src
...
However, the Eclipse Egit wizard provides no obvious way to do that, and insists on creating an undesired subdirectory (screenshots below):
./projectrepo/project/.git
./projectrepo/project/.project
./projectrepo/project/src
...
How can I get Eclipse to add my files at the repository root?
I understand that the Egit manual advises against this, but I do not plan to ever have multiple projects in this repository, so the extra directory is unnecessary (and counts against filesystem path limits on Windows). The fact that it's advised against tells me it must be possible somehow (or the documentation's wrong).
System information:
Windows 7 64-bit
Eclipse Oxygen Java EE (4.7.0 Build id: 20170620-1800)
Eclipse EGit 4.8.0.201706111038-r
Screenshots of Eclipse's current undesired behavior
Configure Git Repository wizard. Most of the things I'd like to change, like Target Location, are uneditable. Path within repository can only be used to add more extra folders. Use or create repository in parent folder of project has a freetext repository path input, but only parent folders will validate and can be used.
Resulting undesired Git repository structure. I would like my project files to be in this directory, not an extra "TestProject" folder:

Try instead to:
import your project into your Eclipse workspace
then use Team/Share project: it should detect the Git repo in it.
That way, you should avoid the double-folder creation.

Related

Where to put Git repository in Eclipse?

If I try to put repository inside project folder
it warns it is not recommended to put git repository inside workspace.
If I try to create intermediate folder
it swears "overlaps the location of another project" at the stage of creating new project.
And if I try to create project inside workspace and repository outside, it moves the project also outside of workspace
So, the only way to use Git under Eclipse it totally abandon workspace?
If you choose the last solution you present, to deal with git and Eclipse, you will not abandon workspace, all your code will remain into your Project Workspace but you will get an additionnal folder somewhere else containing your same code, versioned, a local git repository. By choosing this solution, you can only version some parts of your project and you also avoid some possible conflicts that can arise when versioning your workspace project folder. You can also have a cleaner view of your code if you want it without eclipse metadata and do some complex git tasks on the commandline in this separate folder.

Multi module maven project in a new local git repository?

Yesterday and today i converted my eclipse juno project to multi module maven project in eclipse. I have 1 maven project of type 'pom-root' and all my other projects are modules. The modules have the root project as parent. This works correctly.
A few hours ago i did try to add the root project to egit, by simply right clicking the root project and picking share project -> egit.
I first created a new repository in a new directory, the result was that all my module project became empty and all source files were copied to the repository directory. Gladly i did see that and could recover easily by copying the files back.
Next after the failed attempt i tried to create the repository in the parent folder of project. This failed somehow and the root project became corrupt. After a lot of fiddling around i deleted the corrupt root project and created a new one of type 'pom-root', and somehow i managed to get everything running again.
Next i surfed to stackoverflow ( :-) ) and ask my question:
How can i put my multi module maven project in a new local git repository? (without losing or corrupting everything)
Summary: Delete your Eclipse projects (just their metadata, NOT their contents), then re-create them by importing them from the local repo.
Caveat: I'm at the "a little knowledge is a dangerous thing" stage, so treat this with suspicion.
I just went through this, and I think I have a solution/workaround.
The problem, I believe, is that most of Eclipse still doesn't grok projects nested inside other projects. When you shared your root project, EGit moved all of your project's files out of the Eclipse workspace into the local git repo and then updated Eclipse's metadata for the root project to reflect the new location of the files (good), but (since it is clueless about nested projects) that means it treated all the files in your child modules as though they were part of your root project and hence moved them into the git repo (that's OK) but didn't update the Eclipse project metadata for those child modules (that's bad). So Eclipse shows that you now have a bunch of (child) projects that don't have any files (not even a .project file, nor pom.xml). But if you ignore Eclipse and look directly in the working directory of the git repo, they're all sitting there happily (i.e. EGit copied the directory structure and all the files, including the child modules) and from git's perspective they are all currently untracked.
The solution (I believe) is: delete your Eclipse projects. No, really. But very carefully; see below. [Just to be safe, maybe use e.g. zip to take a backup copy of your lovely files that are sitting in the working directory of the repo. If they were already staged or committed this would be moot, but presumably at this point they aren't].
When you select each Eclipse project and right-click Delete, make sure that the "Delete project contents on disk (cannot be undone)" checkbox is NOT checked -- in other words, you are deleting the Eclipse metadata for each project (I did this for all the child modules and even for the root project) but you aren't deleting the files that are sitting in the working directory of the repo.
(I was worried that m2e might be too "helpful" and, as I deleted each child module, it might remove it from the parent's pom.xml, but thankfully that didn't seem to happen).
In the Eclipse Preferences for Team - Git - Projects, I do have "Auto share projects located in a git repository" enabled. That makes life easier but I assume that you could use less automagic approaches if you preferred.
Once all the relevant Eclipse projects have been deleted, use the Git Repositories view to Import Maven Projects; this creates new Eclipse projects (for the root project and for the child modules) with valid metadata that points to the files in the repo. Hallelujah.
I believe this works because one part of Eclipse that isn't clueless about nested projects is the import-maven-projects logic in m2e.
At this point all the files are still untracked, so next you probably want to select the root project and right-click Team --> Add to Index (which will also add all the child modules, I assume because it's oblivious to the nested projects and just treats them as extra files in the root project).
I wondered what would happen if I then added a new child module -- would it "just work" (and be shared in the git repo) or would it require more gyrations? I was pleased to find that (as far as I can see) it "just works": the Eclipse project metadata shows the files in the right place (in the git repo, as a child under the root project) and marks the project as shared in git.
So my guess is that if you or I had a time machine, the easy way to do this would have been to create the root Maven project, share that to EGit, and only then start creating the child Maven modules.
My environment (though it probably doesn't matter):
Eclipse 4.3.1 (Kepler SR1) with the usual versions of its components: m2e 1.4.0, EGit 3.0.3, m2e EGit connector 0.14.0.

Eclipse workspace and Git folder layout

I am migrating from Eclipse/SVN to Eclipse/Git and I am unsure what the best directory layout would be. I have looked at the Vogel/a guide, the Eclipse Wiki as well as the following threads: Is it better to keep Git repository inside or outside of Eclipse workspace? Should I store git repository in Home or Eclipse Workspace?
I am used to setting up eclipse like so:
~/projectA/workspace/.metadata
~/projectA/workspace/subproj1/.project
~/projectA/workspace/subproj2/.project
~/projectA/subproj1/.svn
~/projectA/subproj1/file1
~/projectA/subproj2/.svn
~/projectA/subproj2/file2
So the project holds all project related files i need. The workspace folder holds relatively little data like the local revision info, project descriptions and specific eclipse settings.
The subproj folder(s) hold all the source code but also the subversion meta info.
The subprojects are more (or less..) related but they all contribute to projectA.
The eclipse working directory is ~/projectA/subproj1, ~/projectA/subproj2, etc.
I am not completely sure if i could use a more or less similar layout with Git. Biggest difference would be the 'repo' dir.
~/projectA/workspace/.metadata
~/projectA/workspace/subproj1/.project
~/projectA/workspace/subproj2/.project
~/projectA/subproj1/.git
~/projectA/subproj1/file1
~/projectA/subproj2/.git
~/projectA/subproj1/file2
~/projectA/repo/subproj1
~/projectA/repo/subproj2
If i set it up like the following, do I still comply with the best practices as mentioned in the Wiki?
#eugener I would assume the following layout complies with the Egit manual?:
~/projectA/workspace/.metadata
~/projectA/workspace/subproj1/.project
~/projectA/workspace/subproj2/.project
~/projectA/subproj1/.git
~/projectA/subproj1/file1
~/projectA/subproj2/.git
~/projectA/subproj1/file2
I would suggest to comply with EGit Wiki simply because the the way Git works.
The main point of Git is fast branching and merging, which is accomplished in repository's workspace, location of which is fixed in the folder where your repo is. Actual repository is located one level down in .git folder.
When project in Eclipse is "shared" with Git all the files are automatically moved from Eclipse workspace where the project was created to the location of the git repo it was shared with. So project in Eclipse is just a shortcut. Branch switching replaces the files in Git workspace which is automatically reflected in Eclipse.

How to create Eclipse project with EGit clone

After spending a decade with SVN I've finally taken the plunge with Git. I have set up Git, Gitolite and GitLab on a server and have successfully added code to my local repository, committed, cloned repositories and pushed code back to repositories. So far, so good. Now enters EGit...
I have cloned a repository using the Git Repository Exploring view using the following syntax for the path:
ssh://dexter:vaultanalyser.git
(In GitLab, repositories are referred to as projects, so I assume that I am supposed to have one repository per Eclipse project? Rather than a SVN-style single parent repository that contains multiple projects?)
This imports the repository into:
/Users/mattpainter/git/vaultanalyser
I was expecting this step to automatically create an Eclipse project for me with all the source, but this isn't so. I tried fudging the target directory so it's in my workspace, but this isn't working either.
How do I get the cloned source available within Eclipse? This site implies that if you create a project with the same name as the repository, it all magically works, but this isn't the case (yes, I know the article is about Github, not GitLab, but I figured the two were close enough for the task at hand).
I've then tried creating a project in Eclipse and sharing it - but then the whole project appears as a sub-folder in the repository. If repositories are indeed analogous to projects, this isn't really what I want.
I've looked through other StackOverflow topics that look related, but I fear I'm still missing a key piece of understanding with how this is supposed to work and it's all looking remarkably convoluted thus far.
Help?
In GitLab, repositories are referred to as projects, so I assume that I am supposed to have one repository per Eclipse project?
Yes, but a GitLab "project" isn't necessarily an Eclipse one.
It doesn't have to follow an SVN structure, as illustrated in "Eclipse reference directory outside eclipse project directory but within repository".
All you need to do is to create an Eclipse project, specifying the source directory being not in the default path (Eclipse workspace), but wherever you cloned your repo (as described in "Getting started with Eclipse + EGit - confused").
That way, the eclipse project you just declared (and referenced in the Eclipse workspace) has its files (.project and .classpath) at the root of the Git repo.
And Egit can then manage that project just fine.
Or you can import it directly with Egit: "Eclipse + EGit: clone project into workspace".
As the OP nullpainter reports below:
The original issue was compounded by invalid permissions in the .git/objects folder on the server.
Running a chmod git:git -R * on the folder solved the issue
He details the right setup below.
To expand on #VonC's answer, the steps to get EGit and Eclipse to play nicely is:
Select 'Clone a Git repository' from EGit, accepting all defaults. This will create a folder in a git parent folder, somewhere outside your workspace.
Create a new Eclipse project. I'm using Java, but I assume there are similar steps for other languages. On the first dialog, untick 'Use default location' and instead select the repository folder created in step 1. Accept all defaults.
Select Team > Share Project... from your new project. Select Git, and tick the 'Use or create repository in parent folder of project'.
Now you can push your code and Eclipse dot files to your git repository from within Eclipse.
(My original issue was compounded by invalid permissions in the .git/objects folder on the server - running a chmod git:git -R * on the folder solved the issues)
I check it out with the command line - then build my projects on top of that. In fact, I end up doing most operations with command line git. EGit is useful for viewing the diffs but I find command line has more power and control. Git is mostly about giving devs lots of power and control.

How to keep .git folder out of a Cloned Eclipse Project

Issue
After importing an Eclipse project from a cloned git repository, I make some changes and commit - and wtf? I get a ".git" folder added to the project, the whole shebang with the heads and refs and worst of all the whole object database gets added to the project, all files/folders having that little question-mark icon signaling that the files have not been added / are not yet tracked by git. This .git folder exists in the actual Working Directory (how does that even make sense?). You can imagine what an annoyance this causes when trying to use the "Synchronize" tool/view (which is supposed to make life easier for committing, you can see all the changes and changed files and diffs).
Question (tl;dr)
How to correctly import an Eclipse project from a cloned git repository? I don't want a .git folder showing up in "Team > Synchronize" when I commit, let the .git folder reside somewhere else outside of my project.
Additional Info
I'm on Windows 7 using Eclipse Indigo and Egit.
I am using Egit to clone a git repo from http://git.apache.org/ (the ofbiz project, to be exact) and in the wizard I choose the option to import an existing project from this newly cloned repo.
Yes I am aware of how little I may expect from Egit. In fact, if there are any alternative ways (external git tool? command line git for windows? other?) to use a git-tracked project in Eclipse which keeps the actual .git stuff out of the project, i'd gladly abandon Egit.
When you clone a git repository, the default behavior is to create a .git folder inside the root of the local clone. You can change the default behavior by setting the GIT_DIR variable:
Git docs says:
"GIT_DIR
If the GIT_DIR environment variable is set then it specifies a path to use instead of the default .git for the base of the repository."
Depending on which terminal you use, you could set it using setenv or export.
For example in a bash terminal:
export GIT_DIR='[path_to_git_directory]'
After setting the variable, you should be able to clone and the .git directory should show up at the specified directory.
I totally agree - I can't imagine it would ever be anyones intent to commit the .git folder!
And git/EGit knows that this is the repository folder of the project, so it should be easy to implement the appropriate exception - so that this folder does not become part of the synchronization.
I know the following does not solve your problem, but in cases where you control the way files are layed out in the repository, you could choose to have the Eclipse project folder not be the root folder of the repository, but rather a sub-folder.
This also allows you to have stuff in the repository that should not show up in Eclipse, or even have multiple Eclipse projects grouped in one repository (if you should wish to do so).