Nishaglobal Education logo

Ignoring files with .gitignore

Keep secrets, downloaded libraries, logs and system files out of your repository with a .gitignore file, learn its patterns, and stop tracking a file you committed by mistake.

Lesson 5 of 1125 minQuiz: 4 questions

🎯 By the end of this lesson you will be able to:

  • Explain which files should never be committed
  • Write .gitignore patterns for files, folders and file types
  • Stop tracking an already-committed file with git rm --cached
Outline
  1. What you'll learn and why it matters
  2. The problem
  3. Create a .gitignore file
  4. When a file is already committed
  5. Practice
  6. Common mistakes
  7. Recap
  8. Quiz
  9. Mark lesson complete

What you'll learn and why it matters

Not every file in your project folder belongs in Git. Passwords and API keys must stay private. Downloaded libraries can be re-installed and are huge. Log files and system files like .DS_Store (Mac) or Thumbs.db (Windows) are just clutter. A .gitignore file tells Git to pretend these files do not exist.

Never commitExamplesWhy
Secrets.env, files with passwords, API keys or tokensAnyone who sees the repository can use them, even after you delete them later
Installed librariesnode_modules/, venv/Huge; anyone can reinstall them from a list
Generated files*.log, dist/, build/Created automatically; they change all the time
Personal and system files.DS_Store, Thumbs.db, editor settingsOnly matter on your computer

The problem

Asha's project now has a secret key in .env, a downloaded library and some log files. Look at the status:

Terminal
git status --short

Output

?? .env
?? debug.log
?? error.log
?? node_modules/

?? means untracked. One careless git add . would commit all of it, including the secret key.

Create a .gitignore file

Create a file named exactly .gitignore (starting with a dot, no other extension) in the top folder of the project, with one pattern per line:

.gitignore
# Secrets: never commit these
.env

# Installed libraries
node_modules/

# Log files
*.log

# System files
.DS_Store
Thumbs.db
Terminal
printf '# Secrets: never commit these\n.env\n\n# Installed libraries\nnode_modules/\n\n# Log files\n*.log\n\n# System files\n.DS_Store\nThumbs.db\n' > .gitignore
git status --short
git add .gitignore
git commit -m "Ignore secrets, libraries and logs"

Output

?? .gitignore
[main 7f7acf1] Ignore secrets, libraries and logs
 1 file changed, 12 insertions(+)
 create mode 100644 .gitignore

Now only .gitignore itself shows up. Commit it, so everyone who works on the project gets the same rules. (In a real project you would type the file in your editor rather than using printf.)

PatternIgnores
.envAny file named .env
node_modules/The folder and everything inside it
*.logEvery file ending in .log
/todo.txttodo.txt in the top folder only
# textNothing: lines starting with # are comments
!keep.logException: do not ignore keep.log, even though *.log matches it

When a file is already committed

.gitignore only affects untracked files. If a file was committed before you ignored it, Git keeps tracking it. Remove it from Git (but keep it on your disk) with git rm --cached:

Terminal
printf 'debug.log\n' > .gitignore
git rm --cached -q debug.log
git status --short
git commit -q -m "Stop tracking debug.log"
ls

Output

D  debug.log
?? .gitignore
debug.log
index.html
styles.css

D means the file is deleted *from the repository*, but ls shows it is still on disk. From now on Git ignores it.

Practice

Guided exercise

Ignore a temp folder and backups

Asha's folder has a temp/ folder and backup files ending in .bak. Write a .gitignore that ignores both, then show the short status.

  1. Create .gitignore with the lines temp/ and *.bak.
  2. Run git status --short: only .gitignore should appear.
Show the solution
Solution
printf 'temp/\n*.bak\n' > .gitignore
git status --short

Output

?? .gitignore

How to get there: The folder pattern ends in / and the wildcard *.bak matches any file ending in .bak. Only the .gitignore file itself is left to commit.

Your turn

Check why a file is ignored

With the .gitignore from the practice above, use git check-ignore -v to find which rule ignores index.html.bak.

Show the solution
Solution
git check-ignore -v index.html.bak

Output

.gitignore:2:*.bak	index.html.bak

How to get there: check-ignore -v prints the file the rule is in, its line number, the pattern and the path it matched. It is the quickest way to debug a .gitignore that does not behave as you expect.

Common mistakes

Naming the file gitignore or .gitignore.txt

You see: Nothing is ignored

Fix: The name must be exactly .gitignore. On Windows, check file extensions are shown.

Adding a file to .gitignore after committing it

You see: Git still tracks the file

Fix: Run git rm --cached <file>, then commit.

Committing a secret and then just deleting it

You see: The secret stays in the history

Fix: Change or cancel the secret immediately, then remove it.

Not committing the .gitignore itself

You see: Teammates still commit the files you meant to ignore

Fix: Commit .gitignore like any other file.

Recap

  • Never commit secrets, installed libraries, generated files or system files.
  • List patterns in a .gitignore file at the top of the project and commit it.
  • Patterns: names, folders ending in /, wildcards like *.log, comments with #, exceptions with !.
  • git rm --cached <file> stops tracking a committed file but keeps it on disk.

Key terms

.gitignore
A file listing patterns of files and folders Git should not track.
Wildcard
A * in a pattern that matches any characters, as in *.log.
Secret
A password, key or token that gives access to something and must be kept private.

Check your understanding

Answer all 4 questions, then check your answers to see an explanation for each one.

  1. 1. Which pattern ignores every file ending in .log?

  2. 2. Why should .env files never be committed?

  3. 3. You added a file to .gitignore, but Git still tracks it. Why?

  4. 4. You pushed a real API key to GitHub. What should you do first?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.