More information about the .gitignore file

.gitignore to specify it in the files and folders you want to hide from the version control system git.

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:
  1. /db.php

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:
  1. # Ignore list of project files
  2. # Ignore all files and directories, including subdirectories and files in them
  3. *
  4. # ---- FILES ----
  5. # Ignoring file type will be ignored in all directories ABSOLUTELY
  6. # For example /files/, /server.log, /uploads/users/data/info.xls
  7. *.zip
  8. *.log
  9. *.pdf
  10. *.xls
  11. # Ignoring files in all directories
  12. # For example /params/db/config.php, /config.php
  13. config.php
  14. # Ignoring a specific file in the project root ONLY
  15. # (The root location of the file is considered .gitignore file)
  16. # For example WILL NOT ignored file /db/config.php
  17. /config.php
  18. # Ignoring ONLY specific file in the specified directory
  19. # For example WILL NOT ignored file /prod/params/config.php
  20. /params/config.php
  21. # ---- DIRECTORIES ----
  22. # Ignore all files and folders in a specific directory ONLY (including subdirectories and files in them)
  23. # For example /images/user.jpg, /images/company/logo.png
  24. # NOT BE ignored files and folders /prod/images/user.jpg
  25. /images/*
  26. # Ignore all files and folders in any directories with the specified name
  27. # For example /images/user.jpg, /core/images/user.jpg
  28. images/*
  29. # Ignore all html-files in a specific directory (not including subdirectories)
  30. # For example /private/index.html
  31. # NOT BE ignored files in /private/ivan/index.html
  32. /private/*.html
  33. # Ignore all html-files to a specific directory INCLUDING subdirectories
  34. # For example /private/info.html, /private/users/ivan/info.html
  35. /private/**/*.html
  36. # ---- MISCELLANEOUS ----
  37. # Exception to ignore
  38. # Ignore all files and folders inside the directory /secret,
  39. # Except for file /secret/free.txt, it will not be ignored
  40. /secret/*
  41. !/secret/free.txt
  42. # Ignoring file with a name containing special characters
  43. # For example !readme!.txt
  44. \!readme!.txt
  45. # Ignoring all JPG and JPEG files within directories,
  46. # that start with "h" and may contain another symbol after
  47. # For example /images/h4/user.jpg, /images/h/company.jpeg
  48. /images/h?/*.jp?g

In repository you can find examples .gitignore-file for your project. For example, if you use Yii framework, then you should pay attention to the file, 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:
  1. git rm --cached <file>

The file is completely gone from the tracked git.

Comments (0)

Leave a comment