The .gitignore file tells Git which files and folders should be ignored and should not normally be included in commits. It is commonly used for temporary files, generated files, local configuration files, dependencies, and sensitive local settings.
.gitignore file affects files that
Git is not already tracking. If a file has already been committed,
adding it to .gitignore does not automatically remove it
from Git tracking.
.gitignore is a special text file used by Git to specify files and directories that should be ignored.
Example:
.gitignore
Git reads the rules written in this file when determining which untracked files should be considered for staging.
Projects often contain files that should not be committed to the repository.
Create a file named:
.gitignore
It is normally placed in the root directory of the Git repository.
my-project/
├── .git/
├── .gitignore
├── index.html
└── style.css
To ignore a specific file, write its name in .gitignore.
config.txt
Git will then ignore an untracked file named
config.txt matching that rule.
You can add multiple file names on separate lines.
config.txt
notes.txt
debug.log
Each line represents a separate ignore rule.
An asterisk can be used to match files with a particular extension.
*.log
This rule can ignore files such as:
error.log
debug.log
application.log
To ignore a directory and its contents, add the directory name.
node_modules/
The trailing slash indicates that the pattern represents a directory.
JavaScript projects commonly use the node_modules
directory for installed dependencies.
node_modules/
Instead of committing the entire dependency directory, projects
usually store dependency information in files such as
package.json and package-lock.json.
Temporary files can be ignored using patterns.
*.tmp
*.temp
This can prevent temporary files from appearing as untracked files.
Log files are often generated automatically by applications.
*.log
This rule can ignore log files throughout matching directories.
Lines beginning with # are comments.
# Node dependencies
node_modules/
# Log files
*.log
Comments help explain why particular files or folders are being ignored.
A path can be used when you want a rule to match a particular location.
/config/local.txt
The leading slash makes the pattern relative to the directory
containing the .gitignore file.
A directory pattern can ignore the directory and its contents.
uploads/
Files such as the following can then be ignored when they are untracked:
uploads/photo.jpg
uploads/document.pdf
uploads/file.txt
The * wildcard can match multiple characters.
*.log
This matches file names ending with .log.
debug.log
server.log
error.log
The ? wildcard can match a single character.
file?.txt
For example, it can match:
file1.txt
file2.txt
Some applications use environment files for local configuration. A project may choose to ignore them.
.env
For example, an environment file may contain local configuration values that should not be committed to a public repository.
Development tools may create project-specific files. For example:
.idea/
.vscode/
Whether these directories should be ignored depends on the project and team requirements.
An exclamation mark can be used to negate an ignore pattern.
*.log
!important.log
This means matching log files are ignored except for
important.log, subject to the normal Git ignore pattern
and directory rules.
# Dependencies
node_modules/
# Logs
*.log
# Environment file
.env
# Temporary files
*.tmp
# IDE files
.vscode/
This is an example of combining different ignore rules in one file.
After creating or changing .gitignore, use:
git status
Ignored files that match the rules normally do not appear as ordinary untracked files.
Git provides a command for checking which ignore rule applies to a particular path.
git check-ignore -v filename
The -v option can show the matching ignore rule and its
source.
Suppose a file has already been committed:
git add config.txt
git commit -m "Add config file"
Adding config.txt to .gitignore later does
not automatically remove it from tracking.
If a tracked file should no longer be tracked, you can remove it from the Git index while keeping the local file:
git rm --cached config.txtThen add the file to
.gitignoreand commit the change.
Git also supports a global ignore file for patterns that you want to ignore across multiple repositories on your computer.
A global ignore file is useful for machine-specific files such as operating-system or editor files.
The global configuration can be set using:
git config --global core.excludesfile ~/.gitignore_global
A project-level .gitignore file is normally committed
to the repository so that the entire team can use the same project
ignore rules.
git add .gitignore
git commit -m "Add gitignore rules"
git status.*.log
*.tmp
*.bak
node_modules/
.env
.vscode/
.idea/
build/
dist/
These are examples only. The correct patterns depend on the technology and requirements of the project.
# Create the file
touch .gitignore
# Add rules
echo "*.log" > .gitignore
# Check repository status
git status
# Add .gitignore
git add .gitignore
# Commit the rules
git commit -m "Add gitignore rules"
The exact command used to create the file can vary depending on your operating system and shell.
Suppose a project contains:
my-project/
├── index.html
├── style.css
├── app.log
├── .env
├── node_modules/
└── .gitignore
The .gitignore file could contain:
*.log
.env
node_modules/
Now Git can ignore the matching untracked files and directory.
The .gitignore file helps control which untracked files and directories Git should ignore.
*.log
.env
node_modules/
It helps keep repositories clean by preventing unnecessary files from being staged and committed.
Remember that .gitignore does not automatically stop
tracking a file that has already been committed.
*.log can match log files.node_modules/ can ignore a dependency directory.# is used for comments.! can negate an ignore rule.git check-ignore -v can help identify the matching rule.git rm --cached can remove a tracked file from the index while keeping the local file.Question: What is the main purpose of a .gitignore file?