Ignoring Files with .gitignore
Not every file in your project should be tracked by Git.
Files such as build output, temporary files, logs, and local
configuration files often do not belong in version control.
Git allows you to exclude these files using a
.gitignore file.
In this lesson, you will learn how to create a
.gitignore file, define ignore patterns,
and verify which files Git tracks.
1. What Is .gitignore?
A .gitignore file contains patterns that tell Git
which untracked files and directories it should ignore.
Git uses these patterns when identifying files to include
in version control.
Common examples of files to ignore include:
- Temporary files and application logs.
- Build output and generated files.
- IDE-specific configuration files.
- Local environment configuration files.
- Dependency folders that can be restored from package managers.
.gitignore does not automatically stop tracking
files that have already been committed. It primarily affects
untracked files.
2. Create a .gitignore File
Create a file named .gitignore in the root
directory of your Git repository.
For example, your project structure might look like this:
├── src/
├── tests/
├── bin/
├── obj/
├── app.log
└── .gitignore
Add patterns for files and folders that should not be tracked.
bin/
obj/
# Log files
*.log
# Temporary files
*.tmp
# Environment configuration
.env
Lines beginning with # are comments.
Each remaining pattern tells Git which matching untracked
files or directories to ignore.
3. Common .gitignore Patterns
| Pattern | Meaning | Example |
|---|---|---|
*.log |
Matches files ending in .log. | app.log |
temp/ |
Ignores a directory named temp. | temp/cache.txt |
*.tmp |
Matches files ending in .tmp. | debug.tmp |
/config.json |
Matches config.json at the .gitignore file's directory level. | config.json |
!important.log |
Negates an earlier matching ignore pattern, subject to parent-directory rules. | important.log |
.env |
Ignores matching .env files according to the pattern's scope. | .env |
4. Verify Ignored Files
After adding patterns to .gitignore, use
git status to check which changes Git reports.
To identify the exact ignore rule that matches a file,
use git check-ignore -v.
If the file matches an ignore rule, Git displays the rule's source and the matching path.
5. Stop Tracking an Already Tracked File
If a file was already committed, adding it to
.gitignore will not remove it from version control.
To stop tracking the file while keeping it on your computer,
use git rm --cached.
Then commit the change:
$ git commit -m "Stop tracking config file"
6. Practical Workflow
- Create a
.gitignorefile in your repository. - Add patterns for temporary files, logs, and build output.
- Run
git statusto review the changes. - Use
git check-ignore -v filenameto inspect matching rules. - Commit the
.gitignorefile so the team shares the same ignore rules.
Key Takeaway
The .gitignore file helps keep generated,
temporary, and local-only files out of your Git repository.
Use clear patterns, verify them with
git check-ignore, and remember that ignoring
a file does not remove it from Git if it is already tracked.