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