Ignore .classpath and .project from Git
Solution 1
If the .project
and .classpath
are already committed, then they need to be removed from the index (but not the disk)
git rm --cached .project
git rm --cached .classpath
Then the .gitignore
would work (and that file can be added and shared through clones).
For instance, this gitignore.io/api/eclipse
file will then work, which does include:
# Eclipse Core
.project
# JDT-specific (Eclipse Java Development Tools)
.classpath
Note that you could use a "Template Directory" when cloning (make sure your users have an environment variable $GIT_TEMPLATE_DIR
set to a shared folder accessible by all).
That template folder can contain an info/exclude
file, with ignore rules that you want enforced for all repos, including the new ones (git init
) that any user would use.
When you change the index, you need to commit the change and push it.
Then the file is removed from the repository. So the newbies cannot checkout the files.classpath
and.project
from the repo.
Solution 2
Add the below lines in .gitignore and place the file inside ur project folder
/target/
/.classpath
/*.project
/.settings
/*.springBeans
Solution 3
The git solution for such scenarios is setting SKIP-WORKTREE BIT. Run only the following command:
git update-index --skip-worktree .classpath .gitignore
It is used when you want git to ignore changes of files that are already managed by git and exist on the index. This is a common use case for config files.
Running git rm --cached
doesn't work for the scenario mentioned in the question. If I simplify the question, it says:
How to have
.classpath
and.project
on the repo while each one can change it locally and git ignores this change?
As I commented under the accepted answer, the drawback of git rm --cached
is that it causes a change in the index, so you need to commit the change and then push it to the remote repository. As a result, .classpath
and .project
won't be available on the repo while the PO wants them to be there so anyone that clones the repo for the first time, they can use it.
What is SKIP-WORKTREE BIT?
Based on git documentaion:
Skip-worktree bit can be defined in one (long) sentence: When reading an entry, if it is marked as skip-worktree, then Git pretends its working directory version is up to date and read the index version instead. Although this bit looks similar to assume-unchanged bit, its goal is different from assume-unchanged bit’s. Skip-worktree also takes precedence over assume-unchanged bit when both are set.
More details is available here.
Solution 4
Use a .gitignore file. This allows you to ignore certain files. http://git-scm.com/docs/gitignore
Here's an example Eclipse one, which handles your classpath and project files: https://github.com/github/gitignore/blob/master/Global/Eclipse.gitignore
Related videos on Youtube
Comments
-
RaceBase about 4 years
I keep myself telling me and others not to commit .classpath and .project files and use Maven.
Somehow, Junior developers always ignore certain rules and commits those files and it's much better to have such files for newbies who can jump and start using the code.
Now from myside, I would like to try/do something. When I clone the repo, I will get .classpath and .project files and certainly they get modified in my system.
But I want them not to be committed and should always be ignored while synchronizing with Git. So that my changes in local system doesn't mess up with Git and Git changes of those files doesn't mess up my local files.
How do I achieve this? Anyway to mark those files to be ignored in such a way?
-
Daniel Schilling over 6 yearsI am a complete Java n00b, but according to Eclipse documentation, those files should be stored in version control. wiki.eclipse.org/…
-
-
RaceBase over 10 yearsI am already using .gitignore. This is applicable if I am committing or creating new repo from my local. If you are cloning the repo, I guess .gitignore will not work, it will fetch .project file and any changes will be out of sync
-
olan over 10 yearsYou need to submit your
.gitignore
to the repo so that everyone uses it. That way, no one should submit those files. .classpath and .project shouldn't ever be in the repo. -
RaceBase over 10 yearswhat about the existing files/repo which contains those files?
-
RaceBase over 10 yearsone more quick doubt, .gitignore in parent folder of repo will do the job or for each project in the repo, we need to have one .gitignore file?
-
olan over 10 yearsThe existing repos will just not be able to submit their versions - otherwise the files won't be affected. Also, you just need one .gitignore file in the root of the project. Be sure to follow the advise VonC gives below to stop tracking the already commited files.
-
RaceBase over 10 yearscan you quickly confirm my doubt about .ignore. ^Above comment
-
Matthew Wise almost 10 yearsI don't see either a .classpath or a .project ignore in that file? Am I missing something? (My .project files won't disappear even though I've got a line for this in my gitignore)
-
Web User almost 7 yearsI am working with a multi-module project. Adding
.classpath
and.project
to.gitignore
is only ignoring the instance in the root directory, but the sub-folders containing.classpath
and.project
are still showing up as changes. I tried adding**/.classpath
to.gitignore
but that does not appear to work. -
GGO about 6 yearsPlease explain what you did editing your answer, avoid only code aswer
-
VIJ about 6 yearsI havent seen anybody answering this question, they just gave some link to refer.
-
Saurabh Patil about 6 yearsI think this answer is super hepful. I knew all the concepts but just wanted the exact code to put in the .gitignore file. Thank you @ViyaanJhiingade And welcome to StackOverflow.
-
Abdollah about 4 yearsWhen you change the index, you need to commit the change and push it. Then the file is removed from the repository. So the newbies cannot checkout the files .classpath and .project from the repo.
-
VonC about 4 years@Abdollah Good point. I have included your comment in the answer for more visibility.
-
VonC about 4 years@Abdollah Why? Once the deletion is committed and pushed (as well as the
.gitignore
), you won't be able to easily add back those files. -
Abdollah about 4 yearsThe PO wants .classpath and .project still remain on the repo. Maybe using SKIP-WORKTREE for such a scenario is a better option. I have explained it here. @VonC Unfortunately, I deleted my previous comment saying "there is no solution in git for such a scenario" some minutes after writing it and I didn't see your response when deleting it.
-
VonC about 4 yearsActually, I have written about it here: stackoverflow.com/a/23806990/6309
-
Abdollah about 4 yearsGreat comparison.
-
IVleafclover almost 4 yearsThis should be the new accepted answer, because it is the only way to keep the files commited to git and ignore them for new commits.