How to initialize git for a new eclipse (neon) java project - eclipse

I installed a fresh copy of eclipse Neon, and created a new gradle java project in a new and shiny workspace. What is the best practice for adding git to the party?
I read that initializing git in the project directory is really a bad idea.
What is a particularly good idea then?!
Thanks!

The good idea is to git init a parent folder of the project different from the main workspace folder.
This is exactly what happens if you let Egit git init your project (Right click on project -> Team -> Share Project -> Git ->...) and select an external folder as repository, say c:\users\john\my-git-repository.
You will then have two folders:
c:\users\john\my-git-repository containing the \.git folder and \<my-project> folder
c:\users\john\<eclipse-workspace> the eclipse workspace folder, which will NOT contain your project folder (remember that eclipse workspace is just a logical container for projects, they don't need to be physically there).
Another option is to create a folder inside the workspace, create the project as a subfolder of that folder and then git init that folder. This way:
c:\users\john\<eclipse-workspace>\shared-projects\<my-project>
You will create the repository in the \shared-projects folder (either by command line with git init or from whithin Eclipse with the wizard), which will contain the \.git folder, \<my-project> folder and any other project you want to share.
Remember why Eclipse suggests to keep repositories outside the workspace (https://wiki.eclipse.org/EGit/User_Guide#Creating_Repositories):
It is a good idea to keep your Repository outside of your Eclipse Workspace.
There are several reasons for this:
The new Repository will consider the complete folder structure of the Eclipse workspace as (potential) content. This can result in performance issues, for example when calculating the changes before committing (which will scan the complete .metadata folder, for example)
If your git repo is in the \shared-projects folder the repository will NOT consider the complete folder structure of the Eclipse workspace as (potential) content and will NOT scan the .metadata folder since it's outside the repo. The only contents of the repo will be your shared projects!

Related

Egit: How to create a git repository that is in a separate folder from the project workspace in Eclipse?

In Eclipse Oxygen, I created a project called testproj at c:\jwork2019\testproj\. My workspace is at c:\jwork2019\. The project's folder has the content of
c:\jwork2019\testproj\.settings\
c:\jwork2019\testproj\src\
c:\jwork2019\testproj\.project
I would like to create a git repo for the project, located at a separate repo directory at c:\jrepo2019\. I successfully created the empty repo c:\jrepo2019\testproj\.git\. I want it to track all the project's files located at the workspace at c:\jwork2019\testproj\*.
I have successfully created the project and the empty repo via Eclipse and Egit.
The problem is when I tried to Configure Git Repository of the project by right-clicking on the project and then Team -> Share Project..., I could select the repository created, but it changes/moves the project's folder location from my workspace at c:\jwork2019\testproj\ to c:\jrepo2019\testproj\testproj\.
I would like the project workspace directory stays in c:\jwork2019 instead of moving the project directory to the repository folder c:\jrepo2019\....
I am guessing that I can do that via git command line by just git add c:\jworks2019\testproj\*, but I don't think I have the git command line installed, I only have git on Eclipse.
I found the question Can I store the .git folder outside the files I want tracked? that has the same goal. But it does it on linux command line.
Is this achievable in eclipse? And how can I do what I wanna do? Thanks

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.

spliting git repository from eclipse project - egit

I'm more or less a git newbie, trying to get a good Setup to work in Eclipse with egit. I'm running Kepler with latest updates.
Originally I Setup the structure inside the Workspace Directory.
+Workspace/.git
+Workspace/TheProject
Using this Setup (admittedly with Juno) commits were taking forever. For this and other reasons I'd like to move git outside the Workspace Directory, outside eclipse in a parallel Directory.
+OtherDirectory/.git
+Workspace/TheProject
But I'm not getting very far. There seems to be no easy way to split the EXISTING git to another Location without having the Project move there too(!?)
I've tried e.g. Cloning the existing git (in the git repository perspective) and having it Import the Project in the same move.
I've tried just Cloning the existing git, without selection of "Import Project" at the same time.
Then I right clicked the cloned repository and went through the Import Existing Project dialog. I don't get a Chance to say where the Project should be loaded to.
In both cases the Project Ends up in the same Directory as the .git Directory (now outside the Workspace)
+OtherDirectory/.git
+OtherDirectory/TheProject
and not as I wish - to have the Project in the Workspace Directory.
This must be possible (?), but how?
Simply copy the .classpath and .project from Workspace/TheProject or OtherDirectory/TheProject, to otherDirectory (where the .git folder is).
Then import your project into the workspace, as in "Eclipse - Import an existing project?"
Everything will remain in OtherDirectory/ (outside your Eclipse workspace folder), but will be visible in your Eclipse workspace.

Eclipse - Exclude root directory from git repository?

I'm trying to setup a git repository for my Eclipse project using EGit. However, I'm having trouble excluding the root/project directory from the repository. That being, my project has the structure:
ProjectDirectory
src
war
etc
I would like the repository to contain src, war, and etc, but not contain the parent directory ProjectDirectory. That's because if I want to clone a copy of the project in my workspace, ProjectDirectory2, the repository for the second project now will try to create a second root directory, ProjectDirectory. Unfortunately, when I try to add a repository for a project using EGit, I seem to only be able to use Team > Share Project on the project folder itself and I don't seem to see anyway to exclude the root directory in the repository from within Eclipse.
Any suggestions on how to skip the top directory in the repository using EGit? I'd prefer being able to do everything from within Eclipse, but if there isn't a way, is there a way to setup the repository this way outside of git, then still be able to use the git control regularly from within git on the repository skipping the top level directory? Thank you much.
Short answer is It is POSSIBLE with EGIT + M2E
trick is when doing share project -> in the configure git repo window
making sure use or create repository in parent folder of project is ticked
and making sure click create repository button
the main aim of this is to create the .git folder in you project root
not above your project root.
Here's my solution. Though not particularly elegant - the steps are all simple, it works, and can be done in about 10 minutes:
Clone the remote git repo into your local filesystem .. e.g.: ~/git/project_repo_root
Create your eclipse project as usual .. e.g.: ~/workspace/eclipse_project_root
Delete the project from eclipse (but not filesystem!)
Open file manager and move the eclipse project you just created from ~/workspace/ root into ~/git
mv ~/workspace/eclipse_project_root ~/git/
Move the .git/ folder from the repo folder to the eclipse project root folder:
mv ~/git/project_repo_root/.git ~/git/eclipse_project_root
Using a shell go into /eclipse_project_root/ and do the usual command line commands for all files you want in the repo:
git add <abc>; .. commit; .. push
Return to Eclipse and go to Git Perspective, and click "Add an existing local Git repo to this view". Select the git repo you are working with and the view should be updated with an entry for that repo once you say ok.
Once you see the local repo in your view, you can right click it and select Import Projects
Go back to your main development perspective (Java, etc) and you should see the project there with the Git markup on the root node in package explorer view.
~~~~~
At that point, you should see the eclipse project folder you had just moved (to ~/git in the examples described here)
I realize this is more verbose than I would like my answers to be, but for this particular challenge/solution .. I don't know of a more straightforward way to do it.
And as for the post above that describes you should always include the eclipse project root directory .. the reality is some projects/clients/etc require that the git repo not contain the root folder. So a solution like this is necessary sometimes
You should commit the project root for multiple reasons:
If you commit only sub directories of your project, you will miss files and folders which are hidden below the project root (e.g. the .settings directory and others). Those need to be committed also, otherwise your project is going to miss information and may not lead to the same results on another machine.
Your wish of trying to clone the same repository a second time in the same workspace sounds very much like you should re-read about git branches. In git, switching between branches is done after a blinking of the eye, so branches should be used when working on different features, versions or otherwise different editions of your project.
The Eclipse project name and the underlying folder name on disk do not need to be the same (watch out for the checkbox "use default location" when creating a new project). So you can checkout a second clone of the project with a different name.
If you later add more projects to your workspace (e.g. a test project), you cannot easily share them into the same git repository, as the content of those additional projects would mix up with the sub directories of the first project in the repository.
If you have maven project you would typical like to create a repository without the project folder created by egit. For my experience this is not possible with the egit plugin.
But it can be easily done from the git command line.
First move your existing eclipse/maven project from your eclipse workspace into your git folder (this is what also the egit plugin did):
mv myproject ~/git/
next create a new git repository
cd ~/git/myproject
git init
Now create a .gitignore file and place it into your new repository folder (~/git/myproject/.gitignore). This is an example for a .gitignore file:
# ignore all bin directories
# matches "bin" in any subfolder
bin/
# ignore all target directories
target/
# ignore all files ending with ~
*~
# ignore eclipse directories and project files
.settings/
.project
.classpath
Finally initalize your new repository with the content of your project
git add *
git commit -m "My initial commit message"
Thats it.
Now you can restart your eclipse and reimport the project from your new git repository.
Go into your eclipse egit repository view and coose 'import exisiting project'.

Import existing mult-rep CVS project folder into Eclipse

I wish to import an existing CVS managed project into Eclipse. I am currently trying to shift my work on to the Eclipse IDE. Some details about my project and environment below.
I'm working in Linux Ubuntu, the project folder is located on a mounted shared network drive, I have installed the "Eclipse CVS Client" plug-in for my version of Eclipse (Helios).
I've tried many ways for eclipse to use my existing folder as a project and recognize the CVS data in the CVS folders.
I have done the following options:
Created a new project, selected existing source, located my project folder and clicked OK to finish creating. In the end the CVS files weren't automatically read.
Did the same as above and after project creation I wen to the option "project menu->team->share project", it asks me to choose a repository and doesn't automatically find the CVS information in the subfolders.
I have set-up both repositories in my eclipse and can browse the repositories through the CVS browser.
My project directory layout is like this:
+-Project Folder (no CVS folder at this level)
+---Repo A folder
+-----CVS meta-info folder is INSIDE, along with all checked out files from Repo A
+
+---Repo B folder
+-----CVS meta-info folder is INSIDE, along with all checked out files from Repo B
+
+-(couple of random files, not in CVS)
Just right-click on your project and click Team -> Share Project. Eclipse will detect that the folder is already shared and resolve the connection with CVS.
Actually I cannot answer your question (I think it should work as you explaned) but I might have a workaround. Could you remove or rename the existing folder and do a fresh check out from your eclipse/CVS repository exploring view?