Configure .gitignore to not upload certain files

5

I'm developing a .Net project and would like to add some files not to upload to Github via .gitignore . I started to research about and saw some solutions, which I did not understand.

So first, I can add all obj and bin files to my .gitignore, is not that a problem?

Second, considering the structure:

-Aplication
| -src 
| | - Domain
| | | - bin
| | | - obj

How would I ignore this whole bin folder and the entire obj folder?

    
asked by anonymous 15.09.2017 / 22:03

3 answers

8

If .gitignore is at the same level as the Application folder then add this to .gitignore :

Aplication/src/Domain/bin/*
Aplication/src/Domain/obj/*

And save the document, check in git, so you should ignore only the contents of the folders.

If you want to skip content and folders too, do this:

Aplication/src/Domain/bin/
Aplication/src/Domain/obj/
    
15.09.2017 / 22:16
4

What you add in the .gitignore file will not be sent to your git repository.

As described in the official document : "The gitignore file specifies intentionally unversioned files that Git should ignore. already being versioned by Git will not be affected. "

So you can create .gitignore at the root of your project, and for your structure, put:

# Linhas iniciadas em # são comentários
src/Domain/bin/
src/Domain/obj/

This will ensure that the bin and obj folders, and consequently all their contents, will not be sent to the server.

    
15.09.2017 / 22:17
2

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 , 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 ):

./.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

    
16.09.2017 / 03:00