.gitignore & Best Practices
.gitignore & Best Practices
Section titled “.gitignore & Best Practices”📄 What is .gitignore?
Section titled “📄 What is .gitignore?”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.
# Create a .gitignoretouch .gitignoregit add .gitignoregit commit -m "Add .gitignore"🎯 Common .gitignore Patterns
Section titled “🎯 Common .gitignore Patterns”# Dependenciesnode_modules/vendor/__pycache__/
# Build outputdist/build/*.exe*.dll
# Environment files.env.env.local*.env
# IDE / Editor files.vscode/.idea/*.swp
# OS files.DS_StoreThumbs.db
# Logs*.log
# Sensitive dataconfig/credentials.ymlsecrets.jsonLanguage-specific .gitignore templates: Visit gitignore.io or GitHub’s .gitignore templates.
📝 Writing Good Commit Messages
Section titled “📝 Writing Good Commit Messages”The 7 rules of a great commit message:
# ✅ Goodgit 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"
# ❌ Badgit 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🚫 Common Mistakes Beginners Make
Section titled “🚫 Common Mistakes Beginners Make”1. Committing large files
# ❌ Don't commit binaries, videos, or large datasets# ✅ Use .gitignore or Git LFS for large files2. Committing sensitive data
# ❌ Never commit passwords, API keys, or secretsgit 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 start3. Not pulling before pushing
git push origin main# ! [rejected] because remote has changes you don't have
# ✅ Fix: pull firstgit pull --rebase origin maingit push origin main4. Committing directly to main
# ❌ Never commit directly to main in a team project# ✅ Always create a branchgit checkout -b feature-name5. Forgetting to add .gitignore
# ❌ node_modules gets committed! Huge repo!# ✅ Add .gitignore before your first commit✅ Git Best Practices Checklist
Section titled “✅ Git Best Practices Checklist”- [ ] 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 releasesIn Simple Words
Section titled “In Simple Words”.gitignoreprevents 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
.gitignorebefore your first commit