Git Best Practices

Git is powerful, but with great power comes great responsibility. Here are best practices that will make you a Git pro and a better team player.

πŸ“ Commit Messages Matter

πŸ“œ The Seven Rules

  1. Separate subject from body with blank line
  2. Limit subject line to 50 characters
  3. Capitalize the subject line
  4. Don’t end subject with period
  5. Use imperative mood
  6. Wrap body at 72 characters
  7. Explain what and why, not how

βš–οΈ Good vs Bad Examples

❌ Bad:

fixed bug

βœ… Good:

Fix navigation menu overflow on mobile

The menu items were wrapping incorrectly on screens
smaller than 768px due to missing flex-wrap property.

🌿 Branching Strategy

πŸͺœ Git Flow

main
 └── develop
 β”œβ”€β”€ feature/user-auth
 β”œβ”€β”€ feature/payment-integration
 └── hotfix/security-patch

🏷️ Naming Conventions

  • feature/ - New features
  • bugfix/ - Bug fixes
  • hotfix/ - Urgent production fixes
  • chore/ - Maintenance tasks

⌨️ Essential Commands

πŸ”„ Interactive Rebase

Clean up your commit history:

git rebase -i HEAD~3

πŸ“¦ Stashing Changes

Save work without committing:

git stash save "work in progress"
git stash pop

πŸ’ Cherry-picking

Apply specific commits:

git cherry-pick abc123

🚫 .gitignore Best Practices

Always ignore:

  • OS files (.DS_Store, Thumbs.db)
  • Editor files (.vscode/, .idea/)
  • Dependencies (node_modules/, vendor/)
  • Build outputs (dist/, build/)
  • Environment files (.env)

βš™οΈ Workflow Tips

⬇️ 1. Pull Before Push

Always sync with remote:

git pull --rebase origin main

πŸ§ͺ 2. Atomic Commits

Each commit should:

  • Fix one issue
  • Pass all tests
  • Be reversible

πŸ” 3. Review Before Committing

git diff --staged

🀝 Collaboration Guidelines

πŸ•΅οΈ Code Reviews

  • Keep PRs small and focused
  • Write descriptive PR descriptions
  • Respond to feedback promptly
  • Test locally before approving

πŸ•ŠοΈ Conflict Resolution

  1. Communicate with team
  2. Understand both changes
  3. Test after merging
  4. Document decisions

πŸš€ Advanced Tips

⚑ Aliases for Productivity

Add to ~/.gitconfig:

[alias]
 co = checkout
 br = branch
 ci = commit
 st = status
 lg = log --oneline --graph --all

πŸͺ Hooks for Quality

Pre-commit hooks for:

  • Linting
  • Running tests
  • Checking commit messages

❗ Common Mistakes to Avoid

  1. Force pushing to shared branches
  2. Committing sensitive data
  3. Large binary files
  4. Meaningless commit messages
  5. Not using branches

🎯 Conclusion

Good Git practices lead to:

  • Cleaner project history
  • Easier debugging
  • Better collaboration
  • Faster onboarding

Start implementing these practices today. Your future self and your team will thank you!

← Back ↑ Top