Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

What .gitignore Does and How to Use It

Write .gitignore rules, verify which pattern matched, and handle a file Git already tracks without deleting your working copy.

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.

What is GitIgnore? How to Create and Use GitIgnore Picture 1

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
/.env

A 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.

What is GitIgnore? How to Create and Use GitIgnore Picture 2

Check what Git will ignore

git check-ignore -v -- app.log
git status --short --ignored

The 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.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.