0/4

Lesson01

beginner

Ignore Files with .gitignore

Goal

Keep generated files and secrets out of version control by listing them in .gitignore.

Not everything in a project folder belongs in its history. Installed packages can be reinstalled, logs are noise, and API keys must never be committed at all.

A .gitignore file lists the paths Git should pretend it cannot see. This repository has collected a log file, a secrets file and a node_modules folder: teach Git to ignore all three.

Objectives

0/4
  1. Create a .gitignore file in the project root. (Current objective)
  2. Commit .gitignore, so the rules belong to the project. (Not started)
  3. .env is not tracked by Git. (Not started)
  4. git status is clean: the log, the secrets and node_modules are all ignored. (Not started)

Your task nowStep 1 of 4

Create a .gitignore file in the project root.

Need help?

Reference

One pattern per line

.gitignore is a plain text file in the project root. Each line is a pattern:

  • debug.log ignores exactly that file
  • *.log ignores every file ending in .log
  • node_modules/ ignores a whole folder, trailing slash and all
  • build/*.css ignores CSS files in build, but nothing deeper
  • !keep.log makes an exception to an earlier pattern

Blank lines are skipped and lines starting with # are comments.

Before and after

$ git status

On branch main
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	.env
	debug.log
	node_modules/

Once those three paths are listed in .gitignore, the same command reports nothing to commit, working tree clean. Git has not deleted anything: the files are still on disk, it has simply stopped offering them.

Three states a file can be in

Tracked

  • Git has it in a commit
  • Changes show up as modified

Untracked

  • Git can see it but has never recorded it
  • Listed by git status every time

Ignored

  • Matched by a pattern in .gitignore
  • Never listed, never staged by git add .

Preparing your workspace…

Repository Graph

Click a commit to inspect it

0 commits

Working Tree

Not a Git repository yet

No unstaged changes

Your working tree matches the staging area.

Staging Area

Files prepared for the next commit

No staged changes

Use git add to prepare files for your next commit.

Repository

Committed history

No commits yet

Use git commit to record staged changes.