Git Best Practices
Mar 5, 24
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
- Separate subject from body with blank line
- Limit subject line to 50 characters
- Capitalize the subject line
- Donβt end subject with period
- Use imperative mood
- Wrap body at 72 characters
- 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 featuresbugfix/- Bug fixeshotfix/- Urgent production fixeschore/- 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
- Communicate with team
- Understand both changes
- Test after merging
- 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
- Force pushing to shared branches
- Committing sensitive data
- Large binary files
- Meaningless commit messages
- 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!