Sub topic 3
Pattern basics: comments, *, ?, and [abc]
This chapter covers the basic building blocks of a .gitignore pattern. It assumes you can run git init, git add, git commit, and git status, and that you know what a repository's working tree is. Anchoring, directory patterns, **, and negation are covered in later chapters.
How Git reads the file
Git reads a .gitignore file one line at a time. Each line is one of three things:
- A blank line, which Git ignores. Use it to separate groups of rules.
- A comment, which starts with
#. - A pattern, which is matched against file and directory names.
A pattern with no slash in it is matched against the name of every file and directory below the .gitignore file, at any depth. So notes.txt ignores notes.txt in the root and in docs/drafts/.
Comments and whitespace
A # only starts a comment at the beginning of a line. A # in the middle of a line is part of the pattern, so don't add trailing comments.
# Local environment files
.env
.env.local # this is NOT a comment, the pattern includes the text after itTrailing spaces are ignored unless you escape them with a backslash. To match a name that begins with #, escape it as \#file.
The `*` wildcard
* matches any number of characters, including none. It does not match a slash, so it never reaches across directory levels.
*.log
build-*
*~*.logmatchesdebug.logandserver.log, at any depth.build-*matchesbuild-1,build-final, andbuild-.*~matches editor backup files such asnotes.txt~.
The `?` wildcard
? matches exactly one character, other than a slash.
file?.txtThis matches file1.txt and fileA.txt. It does not match file.txt (no character to match) or file10.txt (two characters).
Character classes
Square brackets match one character from a set. You can list characters or give a range.
*.[oa]
report[0-9].pdf
temp[!0-9]*.[oa]matchesmain.oandlibutil.a, which are typical compiler outputs.report[0-9].pdfmatchesreport3.pdfbut notreport12.pdf.temp[!0-9]matchestempAbut nottemp5. A leading!inside the brackets means "any character not in this set". Git also accepts^for this.
A complete example
Here is a small file that uses every construct from this chapter.
# Compiled objects and libraries
*.[oa]
# Editor backup files
*~
# Numbered scratch files: scratch1.txt through scratch9.txt
scratch?.txt
# Log files
*.logYou can check the result with a quick experiment:
git init demo && cd demo
printf '# Compiled objects\n*.[oa]\n*~\nscratch?.txt\n*.log\n' > .gitignore
touch main.o libutil.a notes.txt~ scratch1.txt scratch10.txt app.log keep.txt
git status --shortThe output lists .gitignore, keep.txt, and scratch10.txt as untracked. The other files are ignored. scratch10.txt appears because ? matches only one character.
Common errors
| Mistake | What happens | Fix |
|---|---|---|
| `*.log # logs` on one line | The comment text becomes part of the pattern, so nothing matches | Put the comment on its own line |
| Expecting `*` to match a slash | `a*b` does not match `a/b` | Use `**`, covered in a later chapter |
| Using regular-expression syntax such as `.*\.log` | Git uses glob patterns, so this matches different names than intended | Write `*.log` |
| Adding a rule for a file already committed | The file stays tracked | See the chapter on ignoring tracked files |
Exercise
Which of these names does the pattern data[0-9]?.csv match: data5a.csv, data55.csv, data5.csv, data.csv?
Show answer
It matches data5a.csv and data55.csv. The class [0-9] takes one digit and ? takes exactly one more character. data5.csv has no character for ?, and data.csv has neither a digit nor an extra character.
Exercise
A teammate wrote *.tmp # temporary files in .gitignore, and cache.tmp still shows as untracked. Why, and how do you fix it?
Show answer
A # only starts a comment at the beginning of a line, so Git treats *.tmp # temporary files as one pattern that matches nothing useful. Move the comment to its own line and leave *.tmp alone on its line.