.gitignore & .gitattributes - The Complete Guide

Learn .gitignore to exclude files from Git! Use .gitattributes to handle line endings and more!

Introduction

Let's learn how to ignore files we don't want to track with .gitignore!

What You Will Learn

  • What is .gitignore
  • Writing ignore rules
  • Common .gitignore templates
  • .gitattributes

Prerequisites

What is .gitignore?

A file where you list files and folders Git should ignore!

Common Things to Ignore!

These should almost always be in .gitignore!

  • Dependencies: node_modules/, vendor/!
  • OS files: .DS_Store (macOS), Thumbs.db (Windows)!
  • Build outputs: dist/, build/, out/!
  • IDE files: .vscode/, .idea/!
  • Environment variables/secrets: .env!
  • Logs: *.log!

.gitignore Syntax!

# Ignore node_modules folder
node_modules/

# Ignore all .log files
*.log

# Ignore .env file
.env

# Ignore build folders
build/
dist/
out/

# Ignore OS files
.DS_Store
Thumbs.db

# Ignore .vscode (optional - share if needed!)
.vscode/
.idea/

Where to Get .gitignore Templates!

Global .gitignore!

Ignore files across all repos!

git config --global core.excludesfile ~/.gitignore_global

What About Files Already Tracked?

If you added a file to Git, then put it in .gitignore, Git still tracks it! Remove it from Git first!

git rm --cached filename

.gitattributes!

Handles line endings, diffs, etc.! Common setup:

* text=auto eol=lf

Summary

Use .gitignore to keep your repo clean!

Related Articles

Previous Tutorial

Open Source Contributions

Next Tutorial

Let's learn advanced Git! → Advanced Git