90

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?

RaceBase
  • 18,428
  • 47
  • 141
  • 202
  • 5
    I am a complete Java n00b, but according to Eclipse documentation, those files *should* be stored in version control. http://wiki.eclipse.org/FAQ_How_do_I_set_up_a_Java_project_to_share_in_a_repository%3F – Daniel Schilling Sep 11 '17 at 18:58
  • @DanielSchilling This comment is probably more important than all the answers taken together… – Clément Jan 20 '22 at 14:26
  • 1
    @Clément Agreed. In addition to [my answer below](https://stackoverflow.com/a/18955478/6309), I tried before (in 2010) to argue that [those files should be versioned](https://stackoverflow.com/a/2819639/6309). – VonC Jan 20 '22 at 14:38

5 Answers5

136

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 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. – Abdollah Mar 09 '20 at 17:14
  • 1
    @Abdollah Good point. I have included your comment in the answer for more visibility. – VonC Mar 09 '20 at 17:23
  • @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. – VonC Mar 09 '20 at 21:08
  • The 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](https://stackoverflow.com/a/60616342/3955129). @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. – Abdollah Mar 10 '20 at 11:19
12

Add the below lines in .gitignore and place the file inside ur project folder

/target/
/.classpath
/*.project
/.settings
/*.springBeans
Roman
  • 4,922
  • 3
  • 22
  • 31
VIJ
  • 1,516
  • 1
  • 18
  • 34
  • Please explain what you did editing your answer, avoid only code aswer – GGO Mar 02 '18 at 08:53
  • 1
    I havent seen anybody answering this question, they just gave some link to refer. – VIJ Mar 03 '18 at 10:19
  • 2
    I 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. – Saurabh Patil Apr 08 '18 at 15:22
11

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.

Abdollah
  • 4,579
  • 3
  • 29
  • 49
1

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

olan
  • 3,568
  • 5
  • 24
  • 30
  • I 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 – RaceBase Sep 23 '13 at 08:58
  • 2
    You 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. – olan Sep 23 '13 at 09:01
  • 1
    what about the existing files/repo which contains those files? – RaceBase Sep 23 '13 at 09:05
  • one 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? – RaceBase Sep 23 '13 at 09:07
  • The 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. – olan Sep 23 '13 at 09:10
  • 1
    can you quickly confirm my doubt about .ignore. ^Above comment – RaceBase Sep 23 '13 at 09:12
  • 1
    I 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) – Matthew Wise Jun 09 '14 at 14:49
  • 2
    I 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. – Web User May 03 '17 at 20:56
0

Please add the below lines in .gitignore, It will be available under project cloned folder. Here I consider Project cloned folder name as Code.

/.metadata/
/.recommenders/
/target/
Code/**/.classpath
Code/**/.project
Code/**/*.class
Code/**/*.png
Code/**/.settings
Code/**/bin

for reference: https://www.atlassian.com/git/tutorials/saving-changes/gitignore

In this way we don't need to run any commands, Those mention files will not list under unstaged changes in the git or eclipse git.

Prakash
  • 630
  • 3
  • 10
  • 20