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.


As commented by Abdollah

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.

Answer from VonC on Stack Overflow
Top answer
1 of 5
138

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.


As commented by Abdollah

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.

2 of 5
14

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.

🌐
Chief Delphi
chiefdelphi.com › technical › programming
Help with GitIgnore! Need to ignore .jar and classpath - Programming - Chief Delphi
November 22, 2018 - Ive noticed that each time i pull ... in the computers software. Obviously, i need to ignore these but when i add .jar and .classpath to my gitignore, it doesnt work. my gitignore file is like this(-- show beginning and end) .jar .classpath This should work, shouldnt it?? Help ...
Discussions

.project, .classpath currently not ignored
This commit seems to be the cause https://github.com/dvcs/gitignore/commit/9a2b4b08832124dcd9d52b81e81cf1cf090a8160#diff-033fe2949017fb8e2dffb216a2bafeb7 It is intentional ? More on github.com
🌐 github.com
4
April 25, 2017
java - Why do people gitignore .classpath and .project? - Stack Overflow
One of my colleague recently created a new project in eclipse ,committed and pushed with built-in git client. After I cloned to my computer and opened with eclipse, I found eclipse creating .classp... More on stackoverflow.com
🌐 stackoverflow.com
Add .classpath to .gitignore
Could someone please add .classpath to the .gitignore file? Or is there a reason why it is not excluded? More on github.com
🌐 github.com
5
February 28, 2012
Any way to get rid of .project .classpath .settings/ files?
Every time I open a Java project, ... .classpath files in my project folder. And yes I know it's Eclipse's behavior to create files in project root folder as mentioned in #466 (comment) But it's really very annoying that the extension creates these files silently without any confirmation, especially with the SCM stuffs, I have to manually add them into .gitignore ... More on github.com
🌐 github.com
61
August 27, 2018
🌐
Reddit
reddit.com › r/git › adding a file to .gitignore that has already been committed
r/git on Reddit: adding a file to .gitignore that has already been committed
June 1, 2016 -

Alright. So in one of my repos we accidentally commited a file (.classpath). Now that file has different content depending on the jre developer is using and some other things.

Long story short, it shouldnt be in the repository.

The problem is now: how to remove it and add it to .gitignore. I cant just add it to gitignore because it's already comitted. Googling has brought me to this conclusion:

  • git rm --cached .classpath

  • add ".classpath" to .gitignore file

  • stage + commit + push

The problem is that this keeps the .classpath file locally, but it deletes it in the repository! So if I now pull the repo, it will delete my .classpath file. Instead of just leaving it alone and ignoring it.

Where am I going wrong?

🌐
GitHub
github.com › toptal › gitignore.io › issues › 345
.project, .classpath currently not ignored · Issue #345 · toptal/gitignore.io
April 25, 2017 - toptal / gitignore.io Public · Notifications · You must be signed in to change notification settings · Fork 685 · Star 8.7k · New issueCopy link · New issueCopy link · Closed · Closed · .project, .classpath currently not ignored#345 · Copy link · gaellalire ·
Author   toptal
🌐
scottizu
scottizu.wordpress.com › tag › why-wont-git-ignore-my-classpath-file
Why won’t Git ignore my .classpath file? | scottizu
You can create a new Text Document, open with Notepad, Save As, type “.gitignore” (with the double quotes), select All Files for the File Type. Put this file in the same directory as the target folder.
🌐
CodingTechRoom
codingtechroom.com › question › ignore-classpath-and-project-files-in-git
How to Ignore .classpath and .project Files in Git Repositories - CodingTechRoom
Solution: Use the command `git rm --cached .classpath .project` to untrack these files. Mistake: Not using the correct syntax in the .gitignore file which can lead to Git still tracking the files.
🌐
javathinking
javathinking.com › blog › ignore-classpath-and-project-from-git
How to Ignore .classpath and .project Files in Git: Prevent Accidental Commits for Junior Developers — javathinking.com
There are two types of .gitignore files: Scope: Applies only to the current Git repository. Use Case: Ignore project-specific files (e.g., logs, build outputs like target/, or environment files like .env). Best For: Files unique to the project (e.g., a docs/ folder specific to this repo). Scope: Applies to all Git repositories on your machine. Use Case: Ignore IDE files (like .classpath, .project), OS-specific files (e.g., .DS_Store on macOS, Thumbs.db on Windows), or editor files (e.g., .vscode/, .idea/).
🌐
softwarecave
softwarecave.org › 2014 › 02 › 21 › git-how-to-ignore-files
Git: how to ignore files | softwarecave
February 24, 2014 - The most common method is to create .gitignore file in the root directory of your working copy, add there several patterns matching the files or directories to ignore (one per line) and commit it so that these rules will be shared across the ...
Find elsewhere
Top answer
1 of 3
11

These Eclipse-specific files make it easier to set up Eclipse and Visual Studio Code for a Java project.

In general, IDE and tool specific files (Eclipse, Jenkinsfile, GitHub workflows settings, etc.) should be shared as long as they are used and maintained. Otherwise, delete them.

Of course, if you use a different IDE than Eclipse and Visual Studio Code and do not use the Eclipse compiler in IntelliJ IDEA, these Eclipse-specific files are useless, but they do no harm. As long as you do not use functions like file or folder links (stored in the .project file), sharing these files does not lead to IDE lock-in.

In Maven and Gradle projects the .classpath file can be derived from the pom.xml or build.gradle file, but settings that cannot be derived like compiler settings (warnings and errors), formatter or save actions settings (which are stored in the project's .settings folder) should be shared so that everyone uses the same.

This also applies to the .project file, as it contains which natures the project has and which tooling is required. If something is missing, a dialog will ask if the missing plug-ins should be installed.

Eclipse puts these files into the project folder and not into the .metadata folder, because they are intended to be shared. But why there are people who do not share these files? Probably because of historical reasons. 15 or 20 years ago, there wasn't Git, Maven and Jenkins. In these days, a Java application was usually built on a developer's computer by manually exporting JARs or at best via some batch/shell scripts. This meant, making the same build on a different computer or even just with a different IDE or IDE version might led to a different result, causing a lot of trouble. IDE agnostic builds was the solution for this problem. Maybe that's why people today still think that everything have to be IDE agnostic and recommend to use Maven or Gradle. But these files are not shared to build a product to be shipped. Hopefully.

2 of 3
3

They're Eclipse specific, so they don't really belong to the project's source code. Developers might be using different IDEs, so Eclipse's .classpath would be useless for someone using IntelliJ IDEA for example.

Since the project most likely uses Maven / Gradle / some other build system, the IDE is capable of generating the classpath based on the pom.xml or build.gradle files, as you noticed. Only if there isn't a build system, and the project is IDE specific, it would be necessary to include those files to make sure the project keeps its meta-data. But that's an unlikely scenario in modern times and real life work situations.

It doesn't usually cause problems to include those (unless there are conflicting project specific configurations from different developers), but they're not necessary either. I don't include them since there's no advantage, and it keeps the root of the project cleaner.

🌐
Medium
medium.com › @manvendrapsingh › dry-with-gitignore-do-not-repeat-yourself-28bfa951bdeb
DRY with .gitignore. Setting up a global igignore | by Manvendra P Singh | Medium
March 19, 2022 - For example, eclipse adds .classpath, .project and many hidden plugin folders usually starting with .org.eclipse.**, IntelliJ adds various folders under .idea , VisualStudio comes with a curse of .vscode folder. It doesn’t stop here, different operating systems create their own folder metadata files, like.DS_Store on Mac and thumbs.db on windows. One can ignore these files by adding rules under .gitignore ...
🌐
Iditect
iditect.com › faq › java › ignore-classpath-and-project-from-git.html
Ignore .classpath and .project from Git
git add .gitignore git commit -m "Add .gitignore file to ignore .classpath and .project"
🌐
GitHub
github.com › Maescool › Catacomb-Snatch › issues › 578
Add .classpath to .gitignore · Issue #578 · Maescool/Catacomb-Snatch
February 28, 2012 - Could someone please add .classpath to the .gitignore file? Or is there a reason why it is not excluded?
Author   Maescool
🌐
GitHub
github.com › redhat-developer › vscode-java › issues › 618
Any way to get rid of .project .classpath .settings/ files? · Issue #618 · redhat-developer/vscode-java
August 27, 2018 - Every time I open a Java project, ... .classpath files in my project folder. And yes I know it's Eclipse's behavior to create files in project root folder as mentioned in #466 (comment) But it's really very annoying that the extension creates these files silently without any confirmation, especially with the SCM stuffs, I have to manually add them into .gitignore ...
Author   redhat-developer
🌐
GitHub
github.com › clojure › java.classpath › blob › master › .gitignore
java.classpath/.gitignore at master · clojure/java.classpath
Examine the Java classpath from Clojure programs. Contribute to clojure/java.classpath development by creating an account on GitHub.
Author   clojure
🌐
Coderwall
coderwall.com › p › thzzka › git-remove-file-from-repository-and-keep-it-locally
GIT: Remove file from repository and keep it locally (Example)
July 19, 2020 - "You must not commit your .project and .classpath files!" But it happens all the time, even though I am sure those files are already added to .gitignore. Anyway the solution to fix this luckily is simple and will be shown in the following using .project as an example.
🌐
Javahotchocolate
javahotchocolate.com › tutorials › git-eclipse.html
Git-Eclipse Peculiarities - classpath - Java Hot Chocolate
This file isn't related to Eclipse, but it's useful in working around problems of the sort we're discussing here. Anyone that's used CVS or Subversion should understand immediately what this will do. Add a file or subdirectory to the list of names in .gitignore.