.gitignore
file
In Git, a .gitignore
file is a text file that tells Git which files and directories to ignore when tracking changes in a repository. You can use a .gitignore
file to exclude files that are generated during the build process, contain sensitive information, or are not relevant to the project.
Where to place the file
-
Root of the repository: The
.gitignore
file should be placed in the root directory of your repository. This ensures that Git applies the ignore rules to all files and directories in the repository. -
Subdirectories: You can also create
.gitignore
files in subdirectories to specify ignore rules that only apply to files in that directory and its subdirectories.
Global ignore file
In addition to your project's ignore file, Git also uses a global ignore file that applies to all repositories on your system. The path to the global ignore file is stored in the core.excludesFile
configuration variable.
Find out the location of the global ignore file:
git config --global core.excludesFile
Example
# Add comments for clarity. Comments start with a hash (#) symbol.
# This is a comment!
# Ignore files with specific names.
Thumbs.db
.DS_Store
# Ignore files with specific extensions. The * is a wildcard that matches
# any characters.
*.log
*.tmp
*.bak
# Ignore directories. The trailing slash is not required, but it better
# indicates a directory for the reader.
logs
temp/
# Ignore files in any directory with a specific name.
# The ** pattern matches any directory, including subdirectories.
**/build/
# Ignore files with a specific name in a specific directory.
docs/*.pdf
# Ignore all files in a directory except for one specific file
# The ! prefix negates the pattern, so the file is not ignored.
config/*
!config/main.conf
# Include a previously ignored file using the ! prefix.
!important.log