I'd like to take a few more considerations than the @GuilhermeNascimento and @BrunoFelipe . They are generic considerations of .gitignore
structure for extra reference / future reference.
As Guilherme said, you can make a statement on the way with *
to get the files. From his example:
Aplication/src/Domain/bin/*
Aplication/src/Domain/obj/*
But this asterisk is more powerful than just all files in a folder . This is due to how .gitignore
is interpreted.
In .gitignore
, you inform a set of paths. These paths are interpreted as glob
of shell
. This allows for some cool things, such as for vim
users:
.*.sw[po]
This indicates that every file hidden / started by .
, whose end is .swp
or .swo
, will be ignored.
In vim
, a swap file is created the moment you edit a file. Then, when editing the file resposta.txt
, a .resposta.txt.swp
is created. If vim
stops unexpectedly (without deleting the temporary file .resposta.txt.swp
) and I reopen the resposta.txt
file, vim
will create .resposta.txt.swo
and suggest saving the unapplied changes from .resposta.txt.swp
.
More about glob
:
Something that is very common is finding to skip compiled files. In the java , I usually put the following:
*.class
*.jar
*.war
But this can be dangerous if you use maven
/ graddle
wrapper . In these cases, it makes sense to commit the wrapper jar . How to do this with .gitignore
? Simple, you can deny a drop by putting an exclamation !
at the beginning of the line:
*.class
*.jar
*.war
!.mvn/wrapper/maven-wrapper.jar
Note that the only jar
that will appear will be what is located in .mvn/wrapper/maven-wrapper.jar
.
It also has another interesting feature: ignoring src/Domain/obj/
and ignoring /src/Domain/obj/
has different effects. Because? Well, let's go to the example.
Suppose we have the following files modified (considering .
the base of the directory git ):
./.gitignore
./src/Domain/obj/marmota.o
./test-project/src/Domain/obj/marmota-teste.o
If the content of .gitignore
is src/Domain/obj/
, the only file that will be accused of change will be .gitignore
itself.
Now, if it is /src/Domain/obj/
, then the files displayed will be .gitignore
and marmota-teste.o
.
When you put the bar at the beginning of the line, you are forcing the file being ignored to be relative to the root where .gitignore
is.
One last detail, you can specify% s of% s by each directory.
For example:
./.gitignore
./src/.gitignore
./resources/.gitignore
The first .gitignore
applies globally. The .gitignore
does not interfere with the src/.gitignore
folder, and also ./resources
does not interfere with the resources/.gitignore
folder