Rules

You find these rules in more depth explained in the official docs.

  1. foo/ matches any folder foo (in any folder inside your git worktree), but no file foo or foo.txt
  2. foo matches any folder foo and file foo (in any folder inside your git worktree) but not files that contain the string, like foo.txt
  3. * matches anything except / and ? (in any folder inside your git worktree). So foo* would match foo.txt and *.txt would also match foo.txt
  4. abc/foo matches /abc/foo but not /bar/abc/foo. Whenever you have a string that contains /(except trailing slash) its always considered from the level of the `.gitignore` file. There is no difference between abc/foo and /abc/foo
  5. **/abc/foo matches /abc/foo and /bar/abc/foo. It matches any path ending with abc/foo in it.
  6. abc/** matches all files and folders with infinite depth. abc/* matches all files and folders in the directory abc.
  7. abc/**/*.json matches all json files in the folder abc or its subfolders. abc/*.json would only match all files in the folder abcbut not in its subfolders.
  8. The prefix ! negates the pattern. Any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded.

Note that empty folders are not tracked by git.

Examples

Given the following folder structure.

Can you guess what is tracked when using the following .gitignore files?


*
!/A
Show AnswerNothing! Because * is saying untrack everything. The folder /A is excluded, but it has no trackable files, so its not tracked!

*
!A
Show AnswerFolder /A / and File /A/A. Because * is saying untrack everything. The folder /A and the file /A/A is excluded. Since the folder /A contains trackable files, its tracked.

*
!A/
Show AnswerNothing! Because * is saying untrack everything. The folder A/ is excluded, but it has no trackable files, so its not tracked!

/*
!/A
Show AnswerFolder /A and its content. Everything in the root directory is untracted. If we exclude the folder A/ and the file A/A, then its tracked, because it contains trackable files.

Lets consider another scenario

How to ignore all folders bar?

Show Answer

bar

How to ignore all json files in folder A?

Show Answer

A/**/*.json

Note that A/*.json would only ignore A/bar.json. Also note that A/*/*.json would only ignore A/foo/test.json

adam