Why do you need to use a .gitignore
file, if you can simply select the files you need to commit and do it?
3 answers
There are several tasks that are most effectively solved using the .gitignore
file:
Do not worry about choosing the right files for indexing (which is
git add
).In a large project, there are often many files that cannot be versioned. Editors and development environments, compilers, debuggers, other tools and the operating system itself contribute to this. It may also be convenient for you to store some intermediate results in the
tmp
folder.The
.gitignore
allows you not to look for the files you need, but to add everything at once or at least specify less. I agree with the comment by Etki , the approach "just choose the right ones" does not scale at all.Make a local config that will not be affected by a pull .
Suppose your project needs a config. The values in it depend on the place of execution, the weather and the mood of the developer.
Option 1: make
local.conf
and make changes if necessary. If you suddenly add it and commit it, then you will have conflicts with pull / push. Or with a pull to you, someone else's config will come.Option 2:
local.conf.example
and ignored bylocal.conf
. The second one is used in the work, the first one is used to form it manually or automatically. Good, convenient and does not interfere with automation. I have configured autotests that are run locally and on the integration server.Protect sensitive information from accidental disclosure .
It happens that you accidentally added and zakommitili keys or passwords from some cloud storage, such as Amazon. And then they push this stuff on GitHub. What should be done in this situation? Very quickly run and change all keys and passwords, because almost certainly at your expense bitcoins are already mined.
If it is so necessary to store sensitive information in the project folder, then you need to put it in a sub-folder, ignored by git.
Quickly clean the project from temporary files .
Suppose you are writing in a compiled language and when building your project a lot of intermediate files are formed (object files, precompilation, that's all). Before each assembly it is necessary to remove them in order to avoid unnecessary links. You can do it manually. You can write a script. And you can just add them to
.gitignore
and do this:git clean -fX
- ignored files will not be listed in the
git status
output - ignored files will not be added to the index , even if their names fall under the mask when calling
git add маска
Why use the .gitignore file
rather, it is not “necessary”, but “it is possible”: it is simply convenient.
If you suddenly have a lot of files that need to be committed, it will be inconvenient to list them.
Therefore, it is easier to specify a list of files that should not be monitored, and everything else should be considered the working files of the project and added in semi-automatic mode.
In the case where the project is cluttered with a large number of logs, automatic build files and other things, it is easier to add them by mask to the .gitignore
file and not worry that they will fall somewhere further than your working directory.
In addition, you can create one more or less universal .gitignore
file to .gitignore
your needs (for your "types" of projects) and copy-paste it from project to project without having to create it from scratch every time.
PS Automation - our everything! (^^,)