gitignore
Creating a .gitignore file is essential for managing which files and directories Git should ignore in your repository. Here are the basics:
1. Purpose of .gitignore
- Prevent accidental commits: Avoid committing sensitive data, temporary files, or build artifacts.
- Keep repository clean: Ensure that only necessary files are tracked by Git.
2. Syntax
- Each line in a
.gitignorefile specifies a pattern for files or directories to ignore. - Comments: Lines starting with
#are comments. - Blank lines: Blank lines are ignored.
3. Patterns
- Wildcards: Use
*to match zero or more characters.- Example:
*.logignores all.logfiles.
- Example:
- Directories: To ignore a directory, include a trailing slash
/.- Example:
build/ignores thebuilddirectory.
- Example:
- Negation: Patterns starting with
!negate a previously ignored pattern.- Example:
!important.logensuresimportant.logis tracked, even if*.logis ignored.
- Example:
- Relative paths: Patterns are relative to the location of the
.gitignorefile.
Cheatsheet
Certainly! Here’s a comprehensive .gitignore cheatsheet, covering patterns from common to advanced use cases:
.gitignore Cheatsheet
Basic Patterns
-
Ignoring Specific Files
# Ignore all .log files *.log -
Ignoring Directories
# Ignore a directory and its contents mydir/ -
Ignoring Files by Name
# Ignore all files named 'temp' temp
Wildcards
-
Single Character Wildcard
# Ignore all files with a single character followed by 'file.txt' ?file.txt -
Zero or More Characters Wildcard
# Ignore all .log files in any directory **/*.log
Negation
-
Negate a Pattern
# Ignore all .log files except important.log *.log !important.log -
Negate a Directory Pattern
# Ignore all directories named 'mydir' but not 'mydir' at root level mydir/ !/mydir/
Relative Paths
-
Relative Path Patterns
# Ignore file at root level /file.txt -
Ignore Subdirectory Files
# Ignore all 'file.txt' in any subdirectory of 'mydir' mydir/**/file.txt
Advanced Patterns
-
Composite Patterns
# Ignore all .log files in any directory but not in 'logs' directory **/*.log !logs/**/*.log -
Ignoring Files in Subdirectories
# Ignore all 'config.json' files except in 'src' directory config.json !src/config.json -
Combining Patterns
# Ignore all 'temp' files, but not in 'build' and 'dist' directories temp !build/temp !dist/temp -
Complex Negation
# Ignore all 'bin' files except in the 'tools' directory bin/ !tools/bin
Common Patterns
-
Operating System Files
# macOS .DS_Store # Windows Thumbs.db desktop.ini -
IDE/Editor Files
# VS Code .vscode/ # IntelliJ IDEA .idea/ *.iml -
Programming Languages
-
Python
__pycache__/ *.pyc -
Node.js
node_modules/ npm-debug.log -
Java
*.class
-
Specific Use Cases
-
Binary vs Directory
# Ignore the binary file 'mydir' at the root /mydir # Do not ignore the directory named 'mydir' !/mydir/ -
Ignoring Specific File Types in Specific Directories
# Ignore all .txt files in 'docs' directory docs/*.txt -
Ignore Files with Specific Extensions Globally
# Ignore all .bak files globally *.bak
Global .gitignore
-
Global Ignoring for All Repositories
# Create a global .gitignore file touch ~/.gitignore_global # Add patterns to the global .gitignore echo '.DS_Store' >> ~/.gitignore_global # Configure Git to use the global .gitignore git config --global core.excludesfile ~/.gitignore_global
Best Practices
-
Global
.gitignore: For ignoring files globally across all repositories, create a global.gitignorefile and configure Git to use it:git config --global core.excludesfile ~/.gitignore_global -
Repository-specific
.gitignore: Place the.gitignorefile in the root of your repository. -
Review regularly: Regularly update your
.gitignorefile to reflect changes in your project structure and environment.
Example .gitignore File
Here is an example of a typical .gitignore file for a Node.js project:
# Node.js
node_modules/
npm-debug.log
# Logs
logs/
*.log
yarn-debug.log*
yarn-error.log*
# OS files
.DS_Store
Thumbs.db
# IDE files
.vscode/
.idea/
*.imlBy following these basics, you can effectively manage which files and directories are tracked by Git in your repository.
If you have a binary and directory of same name
If you have a directory and a binary inside it with the same name, and you only want to ignore the binary, you can achieve this using a combination of the .gitignore patterns.
Example Scenario
Let’s say you have the following structure:
myproject/
├── mydir/
│ └── file.txt
└── mydir (binary file)
.gitignore Configuration
To ignore only the binary file and not the directory, you can use the following patterns in your .gitignore file:
/mydir # Ignore the binary file named 'mydir'
!/mydir/ # Do not ignore the directory named 'mydir'Explanation
/mydirtells Git to ignore themydirfile at the root of the repository.!/mydir/tells Git not to ignore themydirdirectory and its contents.
Full Example
Here’s a complete example of what your .gitignore file might look like:
# Ignore the binary file named 'mydir'
/mydir
# Do not ignore the directory named 'mydir'
!/mydir/Verification
After setting up the .gitignore file, you can verify that the binary file is ignored and the directory is not by running the following commands:
-
Check the ignored status:
git status --ignored -
List all ignored files:
git ls-files --ignored --exclude-standard