how to replace my project with a .git folder - eclipse

I am completely new to Git.
I am working on an eclipse project that uses GitLab. I would like to replace the current project I am using with one of the branches on the GitLab server.
I downloaded the repository as .git folder. Now I am wonder how I can completely change my current project with that .git project. Or if there any other way to do it with commands it would be fine as well.

You should checkout another branch using Git (either on command line or using some Git plugin for Eclipse), not by downloading anything.
git checkout another_branch_name

Related

Creating Spring Initializr Project for existing Git repository in Eclipse

I'm confused how to setup my project correct. There is already a existing git repository named "DatabaseHub" which is only containing the README.md after my first commit. Then I have there my Spring Initializr zip which I want to use for this repository. I named it databaseHub as well. It's a Gradle project.
When I'm trying to create the project with this zip in Eclipse in my git project I fail.
What I tried: Cloned the git repo (so it is in the project explorer), tried to import the extracted initializr zip with the option "Existing Gradle project" but this won't work cause of the name collision.
I don't get what's the right attempt to start this. Just putting all the files manually in the repo doesn't let Eclipse know that it's even a project now.
Eclipse version is 2019-09.
Try and unzip that project elsewhere on your disk.
Then go to your local Git repository (the one with the single README) and do:
cd /path/to/local/Git/repo
git --work-tree=/path/to/unzipped/project add .
git restore -- .
git commit -m "Import project"
The git restore -- . step (with Git 2.23+, August 2019) will make sure your imported files are checked out and visible within your local repository.
Finally, import the Gradle project: Eclipse should detect the Git repository in it (if not, do a Team > Share)

Use git for project without deleting

I have this project in eclipse that has 9000+ lines of code that I have been working on, so I decided it is about time I got version control for it. So I installed egit for eclipse and added a project to a respository. Unfortinitely, when I did this, git moved the project from where it was located to its own location, introducing bugs. So now I have to find a way to take git away from the project (remove project from version control) and make git use the project again, this time without moving it.
Visual representation
this is what happened:
project original source: C://something/something/project
| files
|
v
git repository: C://something/git/project
this is what I need to do:
git repository ----------> original source
eclipse resolves workspace vars it has changed
project uses version control, git does not move the files
git creates a master repository for files that are committed from the main location (C://something/something/project)
Move the project back where it was.
Git repository is nothing but a .git (notice the dot in the front) directory and can be moved together with code as it's not depended on absolute paths in any way.
Egit should be able to pick it up from new location, and if it doesn't, init the repo from the command line.

Does git exclude eclipse project files from a new repo by default?

I have tried pushing several eclipse projects to github repos. Every time I have trouble later cloning or fetching them back into eclipse. The reason seems to be that the eclipse specific project files (.project, .classpath, etc) are not staged when I do a 'git add .' I have tried researching gitignore files but cannot find one created by default. The file in .git/info/exclude also seems unrelated. I've searched StackOverflow and the internet at large and find no mention of this. So obviously I'm the only person this has every happened to or the only dummy who can't figure it out on my own. Do I need to stage those project files explicitly to include them in them in the repo (after adding a whole project with a 'git add .')?
If those Eclispe project weren't in a git repo locally, you can create a git repo at the root directory of your project, and you should be able to add the files with a '?' on them, including the .project and .classpath:
(From Egit User Guide / Create Repository section)
But if the project is already in a git repo, check first in the git command-line those files status. In doubt, always fall back to the CLI (command line interface), with any local git installation (for instance for Windows: msysgit)
The EGit Eclipse option "Import > Git > Projects from Git" should reference all the files, included the .project and .classpath
I'd recommend using Egit instead of the command line in this case. While the command line is typically more powerful, Egit knows better about the logical project structure of an Eclipse project.

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

How can I add current Eclipse project to git with EGit?

I have GAE project in my Eclipse and I want to add it to my local git repo, how can I do that?
Since creating a Git repo within a GAE project is possible through command line, it is possible through Egit:
Follow that Egit tutorial:
For a new repo:
Right click your project, select Team -> Share -> Git.
Select the proposed line and press "Create repository". Press finish
Create the file ".gitignore" in your project with the following content.
All files / directories which apply to the pattern described in this file will be ignored
But you can also add it to an existing local repo by:
making a special branch for your project in your local repo (with just the .gitignore file in it)
clone it where your current project is
add all your current project files in it
import said Git repo into your workspace: See 4.2 Clone existing project.
informatik01 adds in the comment a link to a short article describing how to do it using the command line:
Adding Eclipse Project to Git
cd ~/workspace/my-project
git init .