Table of Contents
A .gitignore file tells Git which untracked files to leave out by default. It is useful for build output, logs, and local settings. Adding a rule does not stop Git from tracking a file already in the repository.
Create .gitignore and add patterns
Create a plain-text file named .gitignore at the repository root. Add one pattern per line. Commit the file if the rules should apply to everyone working on the project.
# Build output at the repository root
/dist/
# Logs and an exception
*.log
!important.log
# Local environment file at the root
/.envA leading slash anchors a pattern to the directory containing this .gitignore. A trailing slash limits a match to directories. A later ! rule can make an exception unless a parent directory is itself excluded. See the Git pattern reference.
Check what Git will ignore
git check-ignore -v -- app.log
git status --short --ignoredThe verbose check identifies the pattern and file responsible for the match. If a file is already tracked and should become local-only, review whether your teammates still need it, then use git rm --cached -- path/to/file. This stages removal from the repository while retaining your local copy. Review the staged change before committing.
Handle secrets separately
If someone committed a credential, rotate or revoke it. An ignore rule does not remove existing copies or past commits. For practice with tracked files and commits, see the Git and GitHub course.
Reader Comments 0
Sign in with email or Google to join the discussion.