Git Best Practices
05.03.2024
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!