Why do we use .gitignore file? Learn about gitignore

Prabhakar Kumar
3 min readNov 27, 2020

A .gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that's what I recommend. However, you can put it in any folder in the repository and you can also have multiple .gitignore files. The patterns in the files are relative to the location of that .gitignore file.

What Files Should be Ignored?

Ignored files are usually platform-specific files or automatically created files from the build systems. Some common examples include:

  • Runtime files such as log, lock, cache, or temporary files.
  • Files with sensitive information, such as passwords or API keys.
  • Compiled code, such as .class or .o.
  • Dependency directories, such as /vendor or /node_modules .
  • Build directories, such as /public, /out, or /dist.
  • System files like .DS_Store or Thumbs.db
  • IDE or text editor configuration files.

warning:- when the default content of the file depending on how the file was created can not be ignored by .gitignorefile.

.gitignore Patterns

.gitignore is a plain text file in which each line contains a pattern for files or directories to ignore.

It uses globbing patterns to match filenames with wildcard characters. If you have files or directories containing a wildcard pattern, you can use a single backslash (\) to escape the character.

Globbing lets you use special characters to match patterns/characters. In the .gitignore file, you can use the following:

  • blank lines can be used for spacing
  • # - marks line as a comment
  • * - matches 0 or more characters
  • ? - matches 1 character
  • [abc] - matches a, b, _or_ c
  • ** - matches nested directories - a/**/z matches
  • a/z
  • a/b/z
  • a/b/c/z

Comments

Lines starting with a hash mark (#) are comments and are ignored. Empty lines can be used to improve the readability of the file and to group related lines of patterns.

Literal File Names

The easiest pattern is a literal file name, for example:

1  .DS_Store

This will ignore any files named .DS_Store, which is a common file on macOS.

Directories

You can ignore entire directories, just by including their paths and putting a / on the end:

1 node_modules/
2 logs/

If you leave the slash off of the end, it will match both files and directories with that name.

Wildcard

The * matches 0 or more characters (except the /). So, for example, *.log matches any file ending with the .log extension.

Another example is *~, which matches any file ending with ~, such as index.html~

You can also use the ?, which matches any one character except for the /.

Negation

You can use a prefix of ! to negate a file that would be ignored.

1 *.log
2 !example.log

In this example, example.log is not ignored, even though all other files ending with .log are ignored.

But be aware, you can’t negate a file inside of an ignored directory:

1 logs/
2 !logs/example.log

Due to performance reasons, git will still ignore logs/example.log here because the entire logs directory is ignored.

Double Asterisk

** can be used to match any number of directories.

  • **/logs matches all files or directories named logs (same as the pattern logs)
  • **/logs/*.log matches all files ending with .log in a logs directory
  • logs/**/*.log matches all files ending with .log in the logs directory and any of its subdirectories

** can also be used to match all files inside of a directory, so for example logs/** matches all files inside of logs.

Conclusion:-

To recap, the .gitignore file is used to tell Git about the files that Git should not track. This file should be placed in the same directory that the .git directory is in.

--

--