You can put a .gitignore file under your working directory (the same level as the .git/ directory) to tell Git which paths to treat as “intentionally untracked.
Note:
.gitignore only affects untracked files.
If a file is already tracked (already committed), adding it to .gitignore will not stop tracking it—you must remove it from the repo (while keeping it locally), e.g.:
git rm --cached path/to/file
Then commit that change.
Exclude Files:
# Ignore a single file name (matches at any depth under this .gitignore) secrets.txt
# Ignore all .log files (matches at any depth under this .gitignore) *.log
# Ignore any directory named node_modules at any depth node_modules/
# Ignore any directory named temp at any depth temp/
# If you only want to ignore the directory in the SAME folder as this .gitignore: # /node_modules/ # /temp/
Exclude All Files In a SpecifiDirectory:
# Ignore all files directly under any directory named bin (at any depth) bin/*
# Explained before bin/
Include Specific Files or Directories:
The ! operator can be used to negate the ignore pattern and include specific files or directories.
gitignore # Ignore everything in the logs directory logs/*
# But include the .keep file !logs/.keep
Using Wildcards and Patterns:
gitignore # Ignore all .txt files in the doc/ directory doc/*.txt
# Ignore all .pdf files in the root directory (but not in subdirectories) /*.pdf
# Ignore all .tar.gz files in any directory *.tar.gz
# Ignore files with a number as the filename *[0-9]*
Comments:
gitignore # This is a comment - it will be ignored by Git
Exclude Folders with Specific Names Anywhere in the Repository:
gitignore # Ignore any folder named build wherever it is in the project **/build/
Exclude Files with Specific Names Anywhere in the Repository:
gitignore # Ignore any file named TODO wherever it is in the project **/TODO
Practical Examples:
gitignore # Ignore Mac system files .DS_Store
# Ignore node_modules in any JavaScript project node_modules/
# Ignore the build output directory /dist
# Ignore log files *.log
# Ignore all .env files .env*
# Include .env.example file !.env.example
# Ignore compiled Python files *.pyc
# Ignore the database file in a Django project *.sqlite3
Remember, the patterns in a .gitignore file are relative to the location of the .gitignore file itself. Typically, you’d place this file in the root directory of your repository so it applies to the entire repo. After editing the .gitignore file, you need to commit it to your repository for the changes to take effect.