A .gitignore file specifies intentionally untracked files that Git should ignore. This helps prevent sensitive, temporary, or unnecessary files from being committed to the repository.
-
Comment lines: Use
#at the start of the line.
Example:# This is a comment -
Blank lines: Ignored and used for readability.
-
Exact match:
To ignore a specific file:file.txt
-
Wildcard (
*):
Matches zero or more characters.*.log # Ignore all .log files temp* # Ignore files starting with "temp"
-
Directories:
To ignore a directory and its contents, add a trailing
/./cache/ # Ignore "cache" directory -
Negation (
!):
To unignore files or directories.!important.log -
Patterns with paths:
Specify relative paths for more precise ignores./config/settings.json # Ignore only in the root **/debug.log # Ignore in all subdirectories
If you modify the .gitignore file to ignore files that are already tracked by Git, those files will remain in the repository until you remove them from tracking.
-
Remove the files from tracking (without deleting them):
git rm -r --cached . -
Add the modified
.gitignoreto the staging area:git add .gitignore
-
Commit the changes:
git commit -m "Updated .gitignore and cleared cache"
Now Git will respect the .gitignore rules and stop tracking the ignored files.
-
macOS:
.DS_Store .AppleDouble .LSOverride
-
Windows:
Thumbs.db ehthumbs.db Desktop.ini
-
Linux:
*~ .nfs*
*.pyc
*.pyo
__pycache__/
.env
.venv/node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.env*.class
*.jar
*.war
*.ear
*.iml
```shell
#### C/C++
```shell
*.o
*.out
*.so
*.a
*.d*.gem
.log
tmp/*.exe
*.test
*.out*.rs.bk
target/
Cargo.lock.git/-
VSCode:
.vscode/
-
IntelliJ IDEA:
.idea/ *.iml -
Eclipse:
.classpath .project .settings/ bin/
*.log
*.tmp
*.swp
*.bak
*.old
*.~lock.*-
Ignore all
.logfiles in the/logsdirectory:logs/*.log -
Ignore everything in the
/tempdirectory except.keep:temp/ !temp/.keep
-
Check ignored files:
View which files are being ignored:git status --ignored
-
Exclude
.gitignoreitself:
Add this to.gitignore:.gitignore
-
Use global
.gitignore:
Configure a global ignore file for your user:git config --global core.excludesfile ~/.gitignore_globalExample content of
~/.gitignore_global:*.bak *.swp