CodeBlanks Logo Icon CodeBlanks

Patterns

Learn the .gitignore pattern syntax.

Matching files with patterns

You can use wildcard characters in .gitignore to ignore multiple files with a single rule. * matches any number of characters, while ? matches exactly one character. These characters allow you to avoid listing every file name separately.

# All temporary files
*.tmp

# One-character variation
file?.txt

Patterns determine which items are ignored based on their names.

Directory and exception patterns

You can ignore a directory by adding / at the end of its pattern. For example, cache/ ignores the directory. To override an earlier rule, add ! at the beginning of a line so that a specific file can be excluded from the ignore rule.

# Ignore a directory
cache/

# Keep a specific file
!cache/keep.txt

For an exception rule to work, the file's parent directories must not themselves be excluded.

Practice Create directory and file patterns

Write two rules in .gitignore that ignore the build/ directory and all files with the .bak extension. Each rule must be on a separate line.

Starter code

terminal
# Klasör kuralı
# Dosya uzantısı kuralı

Sample solution

terminal
# Klasör kuralı
build/
# Dosya uzantısı kuralı
*.bak

Expected output

build/
*.bak

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which pattern is suitable for ignoring both report.txt and backup.txt?

  • file?.log
  • backup/
  • *.txt Correct answer
  • report.txt

Explanation: <code>*.txt</code> matches files with the .txt extension regardless of their names.

2 Use a single-character wildcard

Question
# Yalnızca tek karakter farklılığı olan dosyalar
file____
Answer
# Yalnızca tek karakter farklılığı olan dosyalar
file?.txt

Explanation: <code>?</code> matches exactly one character. Therefore, <code>file?.txt</code> matches names such as file1.txt.

3 Pattern characters

Term Meaning
* Zero or more characters
? Exactly one character
cache/ Ignores a directory
! Overrides an earlier rule

All lessons in Beginner