I have a maven multi-modules project which has a depth of around 5 levels.
I’m now moving to git and I see that a lot of the content of the target folder is captured by git as “Unstaged Changes”.
I’ve googled a lot and searched SO but couldn’t find an answer as to how to get git to ignore the entire directory tree of the target folder.
My .gitignore, which resides at the root of the project, looks like this:
.project
.classpath
.settings/
target/
The strange thing is that the .class files, which reside somewhere under the target tree, do get ignored by the above definitions but files like MANIFEST.MF, pom.properties or my own application properties files which are located somewhere in the target directory tree are not ignored.
I’ve read Git Ignores and Maven targets but that solution does not seem to work for me.
I can also say that when I try to add target/* rule it doesn’t change anything and when I replace target/ with the target/* then the .class files appear as unstaged as well.
I’d really appreciate some guidance as it seems very unlikely to me that the hordes of people using git and maven haven’t already resolved similar issues. Thanks.
One of the reasons might be that you already committed that files to your repository. In that case adding them to
.gitignoredoesn’t prevent git from tracking these files. To fix that you should first remove them from repository like that:(If your project has modules, you need to include the path down to the relevant module’s target:
git rm -r parent_project/module_name/target/)If you want to remove directory from repository but not from disk you can use
--cachedoption:In any case you should then commit that deletion to the repository (you can do it in the same commit with adding
target/to.gitignoreif you wish).