Git & GitHub · Daily Workflow

.gitignore

.gitignore is a list of files Git should pretend not to see: dependencies, build output, logs and secrets.

Intermediate 5 min Lesson 12 of 19Shows Git 2.47 and GitHub.com as of 2026. GitHub changes its layout from time to time; button names stay the same.
Before you start:Commit, Push and Pull
0/19 in Git & GitHub
On this page · 5 min · Intermediate

Learning objectives#

  • Write .gitignore patterns
  • Use GitHub’s templates
  • Stop tracking an already-committed file
  • Keep secrets out of Git

Explanation#

Create a file called .gitignore in the repository root. Each line is a pattern: node_modules/ ignores a folder, *.log ignores every log file, .env ignores the secrets file, !keep.log makes an exception.

GitHub has templates for every language (github.com/github/gitignore). Choose one when creating a repository, or copy it later. dotnet new gitignore creates one for .NET projects.

.gitignore only affects untracked files. If you already committed .env, run git rm --cached .env to stop tracking it (the file stays on your disk), commit, and — because it is in the history — change every secret it contained.

Step-by-step screens#

Drawings of github.com, the Git installer, VS Code and a terminal. GitHub changes its layout from time to time; the button names stay the same. The red numbers match the numbered actions under each picture; names on your computer will differ.

Loading pictures…

Code example: A .gitignore for a Node.js web project#

Text.gitignore Illustrative
# dependencies
node_modules/


# build output
dist/
build/


# logs
*.log


# secrets and local settings
.env
.env.*
!.env.example


# editor and OS files
.vscode/*
!.vscode/extensions.json
.DS_Store
Thumbs.db
Expected output
git status no longer lists node_modules, dist, logs or .env; .env.example (with fake values) is still committed so others know which settings exist.

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. Folders end with a slash.
  2. * matches any characters.
  3. ! re-includes a file that an earlier rule ignored.
  4. # lines are comments.

Real-world use case#

A project without .gitignore uploaded 40,000 files from node_modules; with it, the repository holds only the 30 files the team wrote.

Common mistakes#

Adding .gitignore after committing secrets

The secret is still in the history. Remove it with git rm --cached, and rotate (change) the secret.

Ignoring package-lock.json

Lock files should be committed so everyone installs the same versions.

Best practices#

  • Add .gitignore before the first commit
  • Start from a GitHub template
  • Commit a .env.example
  • Check git status before git add .

Remember this#

.gitignore = files Git ignores.

folder/ *.log .env

Already tracked? git rm --cached.

Practice exercise#

Your task: Stop tracking the already-committed file .env without deleting it from your disk.

Show solution
Shell Illustrative
git rm --cached .env
git commit -m "Stop tracking .env"

Mini quiz#

.gitignore quizMultiple choice Question 1 of 1

Which pattern ignores every log file?

Score so far: 0/0

Want more? Practise all Git & GitHub MCQs · Git & GitHub interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.