Skip to content

.gitignore & Best Practices

A .gitignore file tells Git which files to ignore (not track). Think of it like a “do not pack” list — these files stay on your computer and never go to GitHub.

Terminal window
# Create a .gitignore
touch .gitignore
git add .gitignore
git commit -m "Add .gitignore"

# Dependencies
node_modules/
vendor/
__pycache__/
# Build output
dist/
build/
*.exe
*.dll
# Environment files
.env
.env.local
*.env
# IDE / Editor files
.vscode/
.idea/
*.swp
# OS files
.DS_Store
Thumbs.db
# Logs
*.log
# Sensitive data
config/credentials.yml
secrets.json

Language-specific .gitignore templates: Visit gitignore.io or GitHub’s .gitignore templates.


The 7 rules of a great commit message:

Terminal window
# ✅ Good
git commit -m "Fix login redirect bug when token expires"
git commit -m "Add user profile page with avatar upload"
git commit -m "Refactor auth middleware to use async/await"
# ❌ Bad
git commit -m "fix"
git commit -m "asdf"
git commit -m "."
git commit -m "WIP"

Multi-line commit message:

Capitalized, short (50 chars or less) summary
More detailed explanation if necessary. Wrap at 72 characters.
Explain what and why, not how.
- Bullet points are okay
- Reference issues: Fixes #42

1. Committing large files

Terminal window
# ❌ Don't commit binaries, videos, or large datasets
# ✅ Use .gitignore or Git LFS for large files

2. Committing sensitive data

Terminal window
# ❌ Never commit passwords, API keys, or secrets
git add .
git commit -m "Add config"
# Oops! .env file with API keys was committed!
# ✅ Fix: remove from history (but it's already there!)
# Better: use .gitignore from the start

3. Not pulling before pushing

Terminal window
git push origin main
# ! [rejected] because remote has changes you don't have
# ✅ Fix: pull first
git pull --rebase origin main
git push origin main

4. Committing directly to main

Terminal window
# ❌ Never commit directly to main in a team project
# ✅ Always create a branch
git checkout -b feature-name

5. Forgetting to add .gitignore

Terminal window
# ❌ node_modules gets committed! Huge repo!
# ✅ Add .gitignore before your first commit

Terminal window
- [ ] Create `.gitignore` before first commit
- [ ] Commit often, but each commit is a logical unit
- [ ] Write clear commit messages (imperative mood: "Add", "Fix", "Refactor")
- [ ] Branch for every feature/bug fix
- [ ] Pull before you push
- [ ] Never force push to shared branches
- [ ] Review your own diff before committing (`git diff --staged`)
- [ ] Squash WIP commits before merging
- [ ] Delete branches after merging
- [ ] Use tags for releases

  • .gitignore prevents unwanted files (node_modules, .env, build files) from being tracked
  • Write clear commit messages: “Fix login bug” not “fix”
  • Never commit passwords, API keys, or secrets
  • Pull before push to avoid rejections
  • Always branch for features — never commit directly to main
  • Create .gitignore before your first commit