Usually hide files or folders that automatically generate their contents, or have configuration parameters that may vary from those who work together on projects.
Here is a simple example: you have a file "/config/db.php", it prescribes parameters for connecting to the database, and on your local machine and the server, these parameters will be different.
And if the files are different, then git will be at each pull/push swear that there is a conflict.
Another example: if you are using NetBeans and configure your project is stored directly in the project folder, then you need to close the directory "nbproject", so that other members of your project did not suffer from the fact that you accidentally commit this directory.
This situation can be easily solved using the .gitignore file, which can be placed in the folder "/config/.gitignore" with the following contents:
This one line tells git, it is necessary to ignore the file "db.php", which lies in this directory.
But I would recommend you to place the .gitignore file in the root of your application, so it will always be on hand and there is no need to remember where you left the other such files.
Here are some rules of syntax of this file:
- One line - one rule
- Blank lines are ignored,
- Comments available through sign (#) at the beginning,
- "/" Character at the beginning of a line indicates that the rule applies only to files and folders, which are located in the same folder as the file itself. Gitignore,
- Available to use special characters: asterisk (*) replaces any number of characters (zero or more), a question (?) Replaces zero or one character. Can be placed anywhere in the rules
- Two asterisks (**) are used to specify any number of subdirectories, see details below in the examples,
- The exclamation point (!) At the beginning of a line indicates the inversion rules needed to specify exceptions to ignore rules,
- "\" Character is used to escape special characters, for example, to match a file named "!readme!.txt", to write such a rule: "\!readme!.txt",
- To ignore all directories, usually ending in a slash (/), otherwise generally considered to be the file name.
Here is a vivid example of the .gitignore file:
- # Ignore list of project files
- # Ignore all files and directories, including subdirectories and files in them
- # ---- FILES ----
- # Ignoring file type will be ignored in all directories ABSOLUTELY
- # For example /files/data.zip, /server.log, /uploads/users/data/info.xls
- # Ignoring files in all directories
- # For example /params/db/config.php, /config.php
- # Ignoring a specific file in the project root ONLY
- # (The root location of the file is considered .gitignore file)
- # For example WILL NOT ignored file /db/config.php
- # Ignoring ONLY specific file in the specified directory
- # For example WILL NOT ignored file /prod/params/config.php
- # ---- DIRECTORIES ----
- # Ignore all files and folders in a specific directory ONLY (including subdirectories and files in them)
- # For example /images/user.jpg, /images/company/logo.png
- # NOT BE ignored files and folders /prod/images/user.jpg
- # Ignore all files and folders in any directories with the specified name
- # For example /images/user.jpg, /core/images/user.jpg
- # Ignore all html-files in a specific directory (not including subdirectories)
- # For example /private/index.html
- # NOT BE ignored files in /private/ivan/index.html
- # Ignore all html-files to a specific directory INCLUDING subdirectories
- # For example /private/info.html, /private/users/ivan/info.html
- # ---- MISCELLANEOUS ----
- # Exception to ignore
- # Ignore all files and folders inside the directory /secret,
- # Except for file /secret/free.txt, it will not be ignored
- # Ignoring file with a name containing special characters
- # For example !readme!.txt
- # Ignoring all JPG and JPEG files within directories,
- # that start with "h" and may contain another symbol after
- # For example /images/h4/user.jpg, /images/h/company.jpeg
In repository https://github.com/github/gitignore you can find examples .gitignore-file for your project. For example, if you use Yii framework, then you should pay attention to the file https://github.com/github/gitignore/blob/master/Yii.gitignore, it lists the main directories that should be excluded from the git-repository.
Some may ask:
"I added in .gitignore file/directory, but after pushing to the repository there remained ignored file/directory, and if you do git clone, then the ignored file/directory also clone.".
The answer is: It is necessary to remove from the repository ignored files/directory, this is done using:
- git rm --cached <file>
The file is completely gone from the tracked git.