Gitignore Setup Project
What you'll build
A repository with a comprehensive .gitignore that excludes node_modules, environment files, build output, and OS-specific files. You will verify the ignore rules work by checking git status.
Goals
- Create a .gitignore file with common patterns
- Understand wildcard and directory-specific patterns
- Verify that ignored files do not appear in git status
- Commit the .gitignore so it is shared with the team
Prerequisites
- Git installed and basic init/add/commit workflow
Steps
- 1
Set up the project
Create a new repository and add some files that should be ignored: a node_modules folder, an .env file, and a build directory.
Codemkdir gitignore-project cd gitignore-project git init mkdir node_modules build echo "secret" > .env echo "{}" > node_modules/lib.json echo "compiled" > build/app.js echo "console.log('hi')" > index.jsExpected result
A repo with index.js (to keep) and node_modules, .env, build (to ignore).
- 2
Create the .gitignore file
Write a .gitignore with patterns for dependencies, secrets, build output, and OS files. Each line is a glob pattern.
Codecat > .gitignore << 'EOF' # Dependencies node_modules/ # Environment variables .env *.env # Build output build/ dist/ # OS files .DS_Store Thumbs.db # Logs *.log EOFExpected result
A .gitignore file with ignore patterns for common file types.
- 3
Verify the ignore rules
Run git status to confirm that node_modules, .env, and build/ do not appear. Only index.js and .gitignore should show as untracked.
Codegit statusExpected result
Only index.js and .gitignore appear as untracked files.
Hint
If ignored files still show, check the pattern syntax — a trailing slash means directory only.
- 4
Commit the .gitignore
Stage and commit the .gitignore and index.js. The .gitignore itself should be committed so all team members share the same ignore rules.
Codegit add .gitignore index.js git commit -m "Add .gitignore and entry point"Expected result
Two files committed; ignored files are excluded from the repository.
- 5
Verify repository state
Use git status and git log to confirm the repository is clean and the commit history matches expectations. Before any destructive operation, checking the current state is a Git best practice.
Codegit status # Should show: nothing to commit, working tree clean git log --oneline -5 # Review the last 5 commits to confirm history git branch -v # List branches with last commit infoExpected result
git status shows a clean working tree; git log shows the expected commit history.
Try it yourself
What you learned
You created a practical .gitignore with patterns for dependencies, secrets, build output, and OS files. A good .gitignore is the first line of defense against committing sensitive data and junk files.
Related Resources
Related Lessons
- .gitignore
Learn .gitignore pattern syntax.
Related Tools
- .gitignore Generator
Generate .gitignore files for any project type.
Related Cheatsheets
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.