AI Hints
Home›Git & GitHub Tutorial›Safety Nets
Git & GitHub · Lesson 34 of 144

Ignoring Files

.gitignore.

⏱ 2 min read▶ Video at 36:27🎓 Free
Watch this lesson in the Git & GitHub courseWatch on YouTube ↗Full Git & GitHub course

Some files should never go into Git. Passwords and secret keys. Log files. Huge folders of downloaded packages. A file called dot git ignore tells Git to pretend they don't exist.

Ignoring Files
From the video: Ignoring Files
  • Secrets, logs, downloaded packages
  • Should never be committed
  • .gitignore hides them

Files we want to hide

Our project now has a secrets file called dot ENV, a log file, and our real code. Status lists all of them as untracked. We don't want two of them ever committed.

git
ls -a
.
..
.env
.git
about.html
app.js
debug.log
index.html
style.css
git status --short
?? .env
?? app.js
?? debug.log

Writing .gitignore

So we create a file called dot git ignore. Each line is a pattern. Dot ENV ignores that exact file. Star dot log ignores every file ending in dot log. A name with a slash at the end ignores a whole folder. And lines starting with a hash sign are comments.

.gitignorepython
# Secrets
.env

# Logs
*.log

# Folders
node_modules/
.DS_Store

Now they disappear

Let's check status again. The secrets file and the log file have vanished. Only our code, and the dot git ignore file itself, are left. We commit the ignore file, so everyone on the team ignores the same things.

git
git status --short
?? .gitignore
?? app.js
git add .gitignore
git commit -m "Add .gitignore"
[main 3879bae] Add .gitignore
 1 file changed, 4 insertions(+)
 create mode 100644 .gitignore

Summary

A dot git ignore file lists patterns of files Git should ignore. Commit it, so the whole team shares it. Next, what to do when you ignore a file too late.

Key takeaways
  • .gitignore lists patterns
  • *.log, folder/, .env
  • Commit .gitignore
Next up: Ignored Too Late — git rm --cached