What pattern does .gitignore follow? What pattern does .gitignore follow? shell shell

What pattern does .gitignore follow?


gitignore use of '*' follows the glob convention:

Git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will not match a / in the pathname.

For example, "Documentation/*.html" matches "Documentation/git.html" but not "Documentation/ppc/ppc.html" or "tools/perf/Documentation/perf.html".

The OP Lone Learner asks in the comments:

Does the shell also use fnmatch(3) to process glob patterns?
In that case why does * not match zero characters before. (i.e. hidden files) but gitignore does?

Because that is a shell configuration choice.

Type (using shopt, which is specific to bash):

shopt -s dotglobecho *.foo.foo foo.foo

That is using dotglob

If set, Bash includes filenames beginning with a '.' in the results of filename expansion. (for pattern matching)


.gitignore simply contains entries for the pattern you wish to tell git not to track (wildcards support).

This configuration is stored at a folder level.
In there you can specify which file to ignore.
This file will apply to all the subfolders inside in a recursive way.

.gitignore is collecting information in a commutative way:

  • System level - (Global) for example if you are a unix user and you wish to ignore all the *.so files for all your projects you will place it in the global file

  • Local - Usually will be at a project level applying for all the content of the given project.

  • Per folder - specific definitions for the given folder (for example - ignore password file, log file etc or any other resource you wish to ignore)

There is also a configuration property core.excludesfile which allow you to set an ignored file to use as well.

git config --global core.excludesfile ~/.gitignore

You can also specify which files not to ignore by adding the ! before the file so it will not be ignored (read more below about the wildcards whic can be used).

To learn about the syntax you can read about it here.
You can use 3rd part tools to generate this file for you gitignore.io.


I couldn't find what in the page explains that * matches zero or more characters and matches hidden files too

Each line in the file can contain any pattern with the following wildcards:

? = Matches zero or one occurrence of the given patterns.
* = Matches zero or more occurrences of the given patterns.
+ = Matches one or more occurrences of the given patterns.
@ = Matches one of the given patterns.
! = Matches anything except one of the given patterns.


In your comment you asked for information about the * pattern.

Here you can see that the * ignore all files. Git doesn't care if its a hidden file or not. its simply looking for the matched pattern.

enter image description here

As explained above git try to match the pattern you supply in the .gitignore file so if you wish to ignore files in an inner folder you will have to specify the inner folder. Here is a demo of the folder content and how to ignore inner files. You can see that the inner folder aa contain file inside but since the ignore file contains the **/ pattern it will ignore all the inner folders and files.

enter image description here