Git & GitHub·Set up and understand Git

Ignoring files and large files

Keep secrets, logs and build output out of your repository with a .gitignore file.

Beginner 11 min read Lesson 5 of 480 Challenge included
What you will be able to do
  • Write .gitignore patterns for secrets, logs and build output
  • Explain why build output should not be committed
  • Test whether a pattern matches with git check-ignore
  • Untrack an already-committed file with git rm --cached

In plain English

A .gitignore file is a list of patterns that tells Git which files and folders to leave out of version control, such as secrets, logs and generated build output. Build output is the compiled or generated files your tools create from your source code, and it can be recreated at any time, so committing it wastes space and drowns real changes in noise. A secret is any private value such as a password or API key that must never be published, because once it is committed it stays in history. A tracked file is one Git is already watching; adding it to .gitignore does nothing until you tell Git to stop tracking it.

Step by step

  1. 1Create a .gitignore file and list one pattern per line.
  2. 2Test a pattern with git check-ignore to confirm it matches the right files.
  3. 3Remember that .gitignore only affects files Git is not already tracking.
  4. 4Untrack an already-committed file with git rm --cached so it stops being watched.
ignore.shTerminal
printf "node_modules/
*.log
.env
" > .gitignore
git check-ignore -v node_modules
git rm --cached .env
git rm --cached -r build
git status

Common mistakes

  • Adding a secret to .gitignore after it was already committed, which does not remove it from history.
  • Committing node_modules or build output, which bloats the repository and hides real changes.
  • Using the wrong wildcard and ignoring far more files than intended.
  • Believing that adding a tracked file to .gitignore will make Git stop watching it.
Your turn

Ignore and untrack build output

Medium

Create a .gitignore that ignores the build folder and all .log files, then stop Git from tracking a build folder that was already committed.

BASH · starter
Check your understanding before moving on.
Knowledge checkQuestion 1

A file is already tracked, and you add it to .gitignore. What else must you do?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: SSH and authentication
Lesson 5 of 480 · 0% of course