.gitignore

You might say

There are files I don't want Git to track, like secret keys and dependency packages. How do I exclude them?

Tell Git which untracked files should stay out of version control.gitignore patterns keep generated files, dependencies, local settings, and secrets from being added by accident. It does not remove files that are already tracked, and it should not replace secure secret management.
Know first
index.html Trackedstyle.css Tracked.env Ignored
×.gitignoreKeep local secrets out of version control
Add .env to .gitignore

A .gitignore file tells Git which untracked files and folders it should ignore. It does not remove a file that has already been committed. If a secret was exposed, rotate or revoke it immediately.

When to use it

  • Ignore dependency folders
    .gitignore
    node_modules/
    .next/
    .env
    .DS_Store
    Untracked files matching the rule are ignored
  • Exclude build output and caches
    $ git statusworking tree cleannode_modules uses 380 MB and is not in the pending list
  • Keep local environment files out of commits
    .env🔒 Keep local only
    Keep secrets local; do not commit them to the remote repository
  • Share project-wide ignore rules
    Add .gitignoreNode
    Template library: github.com/github/gitignore

When NOT to use it

  • Assume it removes a file already committed
    node_modules 380MBgit push →
    A large number of reinstallable dependencies entered the repository; .gitignore should exclude them.
  • Ignore source files just to hide an accidental change
    .env was pushed to the remote.
    ① Go to the service platform nowRotate or revoke the secret
    ② Use BFG / filter-repo Clean up history
    Deleting the current file does not remove sensitive content from historical commits
  • Commit a secret first and add it to .gitignore later
    .env was committed before the .gitignore rule was added…$ git statusmodified: .envTracked files are unaffected by .gitignore; use git rm --cached
  • Use broad patterns without checking what else they match
    .gitignore
    *
    Ignoring all files keeps new pages out of version control
Anatomy
.gitignore node_modules/ Dependency directory, npm install can be installed back at any time .next/ Build the product, npm run build will regenerate .env Key file, see {c:env-var}, should not be submitted to the repository .DS_Store Mac system generated file, has nothing to do with project code
Installed third-party packages; reinstall them from package.json and the lockfile
Build output that can be regenerated by running the build command
May contain secrets such as API keys and should not be committed
A small operating-system file unrelated to the project source code
Variants
Exact
.env
When you want to ignore files with this exact name
Directory
node_modules/
Add a trailing slash to ignore the entire folder
Wildcard
*.log
When all files with the same extension should be ignored
Negate
!keep.log
When an exclamation point should re-include a file ignored by an earlier rule
Typical use cases
node_modules
.gitignore
node_modules/ # Installed dependencies
.next/ # Generated build output
.env # Local secrets
.DS_Store # macOS metadata
Build folder
zsh — my-first-page
# When .gitignore is not written: $ git status Untracked: node_modules/ .next/ .env .DS_Store … (the list is very long) # After writing .gitignore: $ git status Changes not staged: modified: index.html The list only displays project files that need to be processed
Environment file
Create a new repository
Add .gitignore Node ▾
💡 For an existing project, open github.com/github/gitignore, find the Node template, and copy the rules your project needs.
Editor cache
⚠ .env was pushed to the remote repository
Treat the key as compromised immediately
1
Use the service provider to rotate or revoke the key, then create a replacement
2
Coordinate any history cleanup with the team and hosting provider. Rewriting shared history affects collaborators, branches, and open pull requests.
BFG and git filter-repo can help remove a secret from Git history, but forks, caches, and old clones may keep copies. Verify first that the exposed key no longer works.
Further reading