Git Tutorial
- 1. Introduction to Git & VCS
- 2. Installation & Configuration
- 3. Git Architecture & Concepts
- 4. Basic Workflow (add/commit)
- 5. Git Log & History
- 6. Branching Basics
- 7. Merging & Conflict Resolution
- 8. Git Rebasing
- 9. Undoing Changes
- 10. Working with Remotes
- 11. Git Stashing
- 12. Git Tagging
- 13. Git Ignore & Attributes
- 14. Advanced Git Tools
- 15. Best Practices & Workflows
13. Git Ignore & Git Attributes
In any development project, there are files and folders that you do **not** want Git to track. These typically include dependencies (like node_modules), compilation build folders, temporary log files, local database files, or sensitive configuration files containing API keys and passwords.
Git provides two special files to manage this: .gitignore (to exclude files) and .gitattributes (to define path-specific properties).
Excluding Files with .gitignore
A .gitignore file is a plain text file placed in your repository (usually at the root directory). Each line in the file specifies a pattern for files or folders that Git should ignore.
Common Ignoring Patterns
# Ignore a single specific file
secret-keys.txt
# Ignore all files ending with a specific extension (e.g., logs)
*.log
# Ignore an entire directory (must end with a slash)
node_modules/
dist/
.next/
# Ignore files in a specific folder but not its subfolders
/temp/
# Negation rule: Ignore all .pdf files, EXCEPT a specific one
*.pdf
!official-manual.pdfStandard .gitignore Example (Node.js/Next.js)
Here is a typical production-grade gitignore file for a modern JavaScript/Next.js project:
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# next.js build output
/.next/
/out/
# production/local env configurations (contains secrets!)
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
# debug files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# system files
.DS_Store
Thumbs.db.gitignore file only stops Git from tracking **untracked** files. If a file is already in your repository commits, adding it to .gitignore will *not* stop Git from tracking it! To stop tracking an already committed file, you must untrack it first using git rm --cached:git rm --cached sensitive-config.jsonDefining File Attributes with .gitattributes
While .gitignore tells Git *what* to watch, .gitattributes tells Git **how** to treat specific paths or extensions. It is also a plain text file placed in the repository root.
It is commonly used to resolve two major collaborative development issues:
1. Enforcing Consistent Line Endings
Windows computers use Carriage Return Line Feed (CRLF) characters for line endings, while macOS and Linux use Line Feed (LF). This can cause Git to report changes on every single line of a file just because two developers are using different operating systems!
You can configure Git to automatically handle this and convert all file endings to LF in the repository:
# Set default behavior to automatic text line ending handling
* text=auto
# Explicitly declare that text files should always use LF on checkout
*.js text eol=lf
*.css text eol=lf
*.html text eol=lf2. Identifying Binary Files
Tell Git which files are non-text binary files (like images, ZIP archives, or PDFs) so Git doesn't attempt to calculate diffs or merge them automatically:
# Treat all PNG, JPG, and GIF files as binary
*.png binary
*.jpg binary
*.gif binary