Git overriding .project file in eclipse - eclipse

This is is a simple question. When I switch between branches my eclipse files get deleted in my project because they are not in my repository. .gitignore only works when checking in, not when switching branches. How can I keep my .project files in my project filed while using git?
Thanks

If a .project file was committed to the repository before it was added to .gitignore, it won't be ignored. You need to remove it from the repository. Since you probably want to keep it in your working tree, try this:
git rm --cached .project

Related

How do I easily re-clone a multi-module GIT project under Eclipse?

I have been successfully using Eclipse with GIT on multi-module projects. To create these projects, I typically use "File->Import->Existing Maven Project" pointing to my local GIT repository.
However, recently I encountered a problem I hadn't thought about. I was having problems with my GIT repository (don't recall the exact details). I was told by our GIT support team that I should simply delete my GIT repository and re-clone it.
However, after I re-cloned it, I discovered that the multi-module project that I had created with this repository under Eclipse was no longer usable. By re-cloning the repository, I managed to delete all the .settings folders and .project files that Eclipse had created in all my module folders. I had to completely delete the Eclipse projects and start over from scratch. I lost a lot of project specific settings.
In retrospect, I wondered what I should have done differently. I thought about copying and saving all the .settings and .project files/folders. Then restore them after I re-clone the repository. However, there were about 20 of each of those files distributed over many folders. So that would be a tedious and error prone task.
Is there a better way to handle this? Is there a way to tell Eclipse to put these files/folders somewhere outside the GIT repository? If so, how?
You can delete the repository (.git/ folder) but not the workspace folder (this does not heal all problems but most). You can also add the files to git - this might be the best idea for stuff which can not be recreated. In my case most of my projects which do not have Eclipse specific files checked in perfectly materialize from the Git on Maven-import.
The answer above solves part of the problem and it helped me discover the full answer. To "re-clone" in place is not directly supported by GIT. However, it is fairly easy to accomplish (This assumes that your working directory is clean and there is nothing to commit).
> rm -rf <project name>/.git
The next step must be done because GIT does not allow you to clone into an existing project folder. However, you do not want to create a "bare" repository either as it doesn't create all the indexes in the same way as a regular repository does.
> git clone <repo url>/<project name>.git <temp project name>
You may want to specify the branch you were on in the original project folder to minimize the unsynchronized cruft in the steps below. Now you can move the .git folder into the original project folder...
> mv <temp project name>/.git <project name>
> cd <project name>
The next step is optional. It is likely that there will be at least some unstaged and untracked files after this is done. This just lets you know what they are so you do the next steps knowningly.
> git status
The next steps clean up the unsynchronized files.
> git reset --hard
> git clean -fd
And finally clean up the temporary project folder.
> rm -rf ../<temp project name>
I have tried this and it works.

How to ignore IDE settings on Git?

I have below Git information and I would like to ignore settings of my IDE (Eclipse).
modified: myproject/.classpath
modified: myproject/.project
modified: myproject/.settings/com.google.gdt.eclipse.core.prefs
modified: myproject/.settings/org.eclipse.core.resources.prefs
modified: myproject/.settings/org.eclipse.jdt.core.prefs
modified: myproject/.settings/org.eclipse.wst.common.component
modified: myproject/.settings/org.eclipse.wst.common.project.facet.core.xml
modified: myproject/.settings/org.eclipse.wst.validation.prefs
I tried the below statements in my .gitignore file, but it doesn't work for these settings:
.project
.classpath
.settings
*.project
*.classpath
*.settings
/.project
/.classpath
/.settings
.project/
.classpath/
.settings/
*.project/
*.classpath/
*.settings/
I am using Mac OS X and I also added global gitignore file with these settings git config --global core.excludesfile '~/.gitignore', but I'm still getting the above Git update messages when I check with git status. What am I wrong?
If those elements were already committed, you need to remove them first:
git rm --cached .project
git rm --cached .classpath
git rm --cached -r .settings
The --cached option allows them to stay in the working tree, while being recorded for deletion.
Once deleted, they will be ignored.
Once committed, the next changes will be ignored.
A simple .gitignore in myproject/ folder is enough:
.project
.classpath
.settings/
Note the / for .setting folder: that will ignore everything in it.
Following is my .gitignore config for a java web project:
*.class
*.swp
*.cache
*~
bin/**/*
target/**/*
build/**/*
gen/**/*
# tmp source folder
tmp/**/*
# js plugin
WebContent/js_plugin/lib/**/*
After git 1.8.2, If you want to ignore a folder named abc in any folder or subfolder, use following:
**/abc/**/*
So, I suggest you upgrade your git, if it's too old.
By the way, in my opinion, if team is using the same IDE, you should sync the .classpath things to git, ofcause, this is base on you have convention to name your jdk / tomcat / ... things like this. So that,once a jar is added via maven, all people get it after pull.
But, if you commit eclipse config, when push, make sure that change is useful, if not, don't commit, e.g. 2 source folder just change their line orders, in this case you can git checkout -- xxx, to ignore that change, so that won't effect other people.
It worked for me yeah but removing files from cache would be more helpful
#IDE files .gradle .idea idea-module-files *.iml com.springsource.sts.config.flow.prefs org.sonar.ide.eclipse.core.prefs
org.springframework.ide.eclipse.beans.core.prefs
org.springframework.ide.eclipse.core.prefs
.springBeans
eclipsebin
org.eclipse.jdt.core.prefs
org.eclipse.jdt.ui.prefs
NPM packages
**/node_modules
Compiled class file
*.class
Test classes
**/testclasses

Commit selected files to git and move to bitbucket

i have created a repository in bitbucket and i have maven web project (eclipse ide) as in below structure
project
--src/main/java
--src/main/resource
--src/main/webapp
.classpaht
.project
lib
i followed below link to create git repository and i pushed to project to but bucket, where as i have committed only java, resource and webapp files to git and pushed to bitbucket.
http://wangpidong.blogspot.com.es/2012/05/how-to-use-bitbucket-with-egit-in.html
but when i see in bitbucket i have .classpath, .project and lib folder added to the repository, which i never commited the files. i am new to git and bitbucket , i dont know how to fix, or am i doing it wrongly,, help needed
Might be you have used Team->Add to index, followed by commit, hence all the files/folders are added to the repository.
You can check the help available at github (help.github.com/articles/ignoring-files) for info on how to ignore certain files..
just create a .gitignore file in your project home and add these lines into it:
.classpath
.project
lib/
by .gitignore file you tell git that ignore these files or directories

Directories of Java class files found in Git repository cloned from BitBucket, but I can't figure out how to remove them

I created a BitBucket repository of several Eclipse projects, and then used Eclipse with EGit to clone that repository to a new Eclipse workspace to check whether I had put all of the files into BitBucket right.
The projects in the new workspace contained *.class files in their bin directories, and I realized that I had neglected to delete those files from the Eclipse project directories in the original workspace before adding the projects to the repository.
However, the BitBucket web page for my repository doesn't display any bin directories in the various Eclipse project directories, and when I try to remove the *.class files from the repository that I cloned, I get an error message that says, for example, "fatal: pathspec 'EclipseProjects/IndexerUtils/build/uw_solr/CmdLineOption.class' did not match any files".
I assume this means that the class files are not being tracked, but I'm not sure. I don't know Git well enough to figure out how to find out whether they are tracked or not. Their presence does not cause "git status" to say that untracked files were found, but if I try to remove any of them, Git says it doesn't know about them. In the meantime, BitBucket doesn't display any of the class files in its repository, but when I clone its repository, all of the class files are included in the new repository that is created.
Is there a Git command that can tell you whether a specific file is being tracked or not? If the *.class files are being tracked, how do I remove them, since apparently "git rm " doesn't work? If they aren't being tracked, why do they show up when I clone my BitBucket repository? What kinds of basic diagnostic commands does Git have that could help me figure out what state my repository is in?
Thanks,
Mike
do you have a case collision; try setting following and see if you can see and remove file using git rm command.
git config --global core.ignorecase true
Please note this "git rm" removes file from that commit onwards. If you want to remove file thoroughly please consider filter-branch
example: git filter-branch --tree-filter 'rm filename' HEAD

How to safely use git at the 'workspace' level with branches and .metadata?

I have a git repo at the workspace level. i.e. multiple closely related Eclipse projects in one repo.
If I add .metadata to .gitignore then each time I create new branch and checkout I loose my .metadata file and therefore import all the projects manually. This is unpleasant.
Is it safe to store the .metadata file under version control? This is a multi-developer project and JDK versions and perhaps even OSs (in future) may vary. (We're all on Ubuntu at present.)
Are there any other IDE files which shouldn't be comitted?
Thanks,
Chris.
The problem is that the file and/or directory was already tracked by git before you added it to .gitignore:
for a file, it will continue to be tracked, no matter what;
for a directory, files present in this directory at the time you added it to .gitignore will also be tracked.
This means, among others, that if you have a file f which is untracked in branch b1 but you checkout branch b2 in which this file is tracked, git will remorselessly overwrite f.
As mentioned in the previous question, the solution to make git completely ignore such files after "the harm is done" consists of issuing git rm -r --cached and only then adding them to .gitignore. But this needs to be done branch by branch, which means you will still have the problem in the meantime.
Given your situation, you have two choices:
if you can afford to restart "from scratch", do so and put .metadata immediately into .gitignore -- and commit that first, before even committing the rest;
if you cannot afford that, you have no choice but a git filter-branch.
As to other files to ignore with other IDEs, I can only tell for IDEA: .idea and *.iml. No idea for others...