Interview Q&A / Git & GitHub

BeginnerQuick answer1 min readUpdated 3 Oct 2026

How do the patterns in a .gitignore file work?

SHORT ANSWER

Each line is a pattern. * matches any characters except a slash, a trailing slash matches directories, ** matches nested directories, ! negates a pattern, and

Explain it simply

A .gitignore is a list of name tags the doorman should turn away. Patterns are like filename wildcards.

*.log ignores every log file. node_modules/ ignores that folder only. /build with a leading slash anchors it to the repo root. **/temp matches a temp folder at any depth. !important.log re-includes a file that an earlier rule ignored, but it cannot re-include a file whose parent folder is ignored. Later lines override earlier ones.

Example

text
# dependencies
node_modules/
# build output at repo root
/dist
# logs, but keep one
*.log
!keep.log
# env files
.env*

What to say in the interview

"Patterns work like wildcards: for any characters, a trailing slash for directories, a leading slash to anchor at the root, * for any depth, and ! to negate. Later rules win, and I cannot re-include a file inside an ignored folder."

#git#gitignore#patterns

Related questions

Practise more Git & GitHub questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Git & GitHub quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.