Git Ignores and Maven targets
Asked Answered
Z

7

135

Anyone know if it is possible to ignore all the instances of a particular directory in a file structure managed by git.

I'm looking to exclude all the 'target' folders in a maven project with a number of submodules. I know I can explicitly exclude each of them in a top level .gitignore, but I'd really like to be able to specify a pattern like **/target/* there to have it automatically ignore the instance in sub directories?

Is this possible?

Zendejas answered 13/6, 2009 at 23:46 Comment(1)
With what configuration are you making this work? /.settings/ would only ignore 'xxx/.settings/*', not '.settings/*' or 'xxx/yyy/.settings/*': the ignore patterns do not seem to be applied recursively. See also #971965 .Dismay
O
116

It is possible to use patterns in a .gitignore file. See the gitignore man page. The pattern */target/* should ignore any directory named target and anything under it. Or you may try */target/** to ignore everything under target.

Omophagia answered 14/6, 2009 at 0:52 Comment(5)
Thanks! Here for anyone else, is contents for a .gitignore to skip the ususal maven & eclipse suspects. target/* /target/ .metadata tar.gz .classpath .project */.settings/Zendejas
With what configuration are you making this work? /.settings/ would only ignore 'xxx/.settings/*', not '.settings/*' or 'xxx/yyy/.settings/*': the ignore patterns do not seem to be applied recursively. See also #971965 .Dismay
For multi-module projects you may want to use **/target/, but be careful not to exclude legitimate "target" directories in the sourcesBenzofuran
/target/** works in \.git\info\exclude file a well.Hiero
Just tried to commit a java project with "target" subpackage in one of it's modules. :(Jacklighter
C
241

The .gitignore file in the root directory does apply to all subdirectories. Mine looks like this:

.classpath
.project
.settings/
target/

This is in a multi-module maven project. All the submodules are imported as individual eclipse projects using m2eclipse. I have no further .gitignore files. Indeed, if you look in the gitignore man page:

Patterns read from a .gitignore file in the same directory as the path, or in any parent directory

So this should work for you.

Coitus answered 15/6, 2009 at 10:8 Comment(3)
That is a better answer than '/target/'. It does exclude a directory wherever it is located in the tree of directories. But it would not work for files (see #971965 )Dismay
if you continue reading it says "These patterns match relative to the location of the .gitignore file." Your answear is wrong...Traver
I use this approach and use "target/" to ignore all maven build folders, however be careful that you risk to ignore code that is located in packages containing a "target" folder in their path. (In a project with the following classes: src/main/java/org/example/game/archery/bow/LongBow.java and src/main/java/org/example/game/archery/target/Target.java, your would find nothing to hit with your LongBow with such .gitignore configuration :-) )Supertonic
O
116

It is possible to use patterns in a .gitignore file. See the gitignore man page. The pattern */target/* should ignore any directory named target and anything under it. Or you may try */target/** to ignore everything under target.

Omophagia answered 14/6, 2009 at 0:52 Comment(5)
Thanks! Here for anyone else, is contents for a .gitignore to skip the ususal maven & eclipse suspects. target/* /target/ .metadata tar.gz .classpath .project */.settings/Zendejas
With what configuration are you making this work? /.settings/ would only ignore 'xxx/.settings/*', not '.settings/*' or 'xxx/yyy/.settings/*': the ignore patterns do not seem to be applied recursively. See also #971965 .Dismay
For multi-module projects you may want to use **/target/, but be careful not to exclude legitimate "target" directories in the sourcesBenzofuran
/target/** works in \.git\info\exclude file a well.Hiero
Just tried to commit a java project with "target" subpackage in one of it's modules. :(Jacklighter
S
6

As already pointed out in comments by Abhijeet you can just add line like:

/target/**

to exclude file in \.git\info\ folder.

Then if you want to get rid of that target folder in your remote repo you will need to first manually delete this folder from your local repository, commit and then push it. Thats because git will show you content of a target folder as modified at first.

Staphylococcus answered 10/11, 2016 at 9:29 Comment(1)
The additional action set me straight. I added target/ but the directory continued to exist in the repo. Once removed from the repo, the target folder was never re-added.Jarry
M
4

add following lines in gitignore, from all undesirable files

/target/
*/target/**
**/META-INF/
!.mvn/wrapper/maven-wrapper.jar

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/build/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
Maltha answered 20/5, 2020 at 18:51 Comment(0)
I
2

I ignore all classes residing in target folder from git. add following line in open .gitignore file:

/.class

OR

*/target/**

It is working perfectly for me. try it.

Isidor answered 18/12, 2018 at 13:55 Comment(0)
P
0

For the Spring Tool Suite, Add the following ignore resources to .gitignore file and commit into the repository

*.classpath
*.class
*.prefs
*.jar
*.lst
*.project
*.factorypath
*.original
target/
.settings/
.apt_generated
.sts4-cache
.springBeans
Prescience answered 17/1, 2022 at 12:56 Comment(0)
B
0

For multi-module repositories, in addition to @Gleidosn answer I would add the following expression to take into consideration the nested target folders:

**/target/
Backandforth answered 8/11, 2023 at 9:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.