Git Workflow Best Practices for Professional Teams
Master Git with proven workflows, commit conventions, and strategies used by top development teams.
Git is more than version control - it's communication. Your commit history tells a story. Let's make it a good one.
The Golden Rules
- Commit early, commit often - Small, focused commits
- Write meaningful messages - Future you will thank you
- Never commit to main directly - Use branches
- Review before pushing - Check your changes
- Pull before you push - Stay synchronized
Branch Naming Convention
# Feature branchesfeature/user-authenticationfeature/payment-integration
# Bug fixesfix/login-redirect-issuefix/memory-leak-dashboard
# Hotfixeshotfix/critical-security-patch
# Refactoringrefactor/database-queriesrefactor/component-structure
# Chores (maintenance)chore/update-dependencieschore/configure-ci
# Documentationdocs/api-endpointsdocs/setup-guideCommit Message Format
Use Conventional Commits:
# Format<type>(<scope>): <subject>
<body>
<footer>
# Examplesfeat(auth): add Google OAuth integration
Implemented OAuth 2.0 flow for Google sign-in.Users can now authenticate using their Google accounts.
Closes #123
fix(dashboard): resolve memory leak in chart component
The chart component was not properly cleaning up event listeners,causing memory to accumulate on route changes.
- Added cleanup in useEffect return- Removed unused Chart.js instance
Fixes #456
perf(api): optimize database queries
Reduced query time by 60% by adding proper indexesand implementing query batching.
- Added index on user_id column- Implemented batch loading for related data- Reduced N+1 queries
refactor(components): extract reusable Button component
chore(deps): update Next.js to v15
docs(readme): add deployment instructionsCommit Types
| Type | Description | Example |
|------|-------------|---------|
| feat | New feature | feat(auth): add 2FA |
| fix | Bug fix | fix(api): handle null users |
| docs | Documentation | docs(readme): update setup |
| style | Formatting | style: fix indentation |
| refactor | Code restructure | refactor: simplify logic |
| perf | Performance | perf(db): add indexes |
| test | Tests | test(auth): add unit tests |
| chore | Maintenance | chore: update deps |
Feature Branch Workflow
# 1. Update maingit checkout maingit pull origin main
# 2. Create feature branchgit checkout -b feature/new-dashboard
# 3. Make changes and commitgit add src/components/Dashboard.tsxgit commit -m "feat(dashboard): create Dashboard component"
# 4. Push to remotegit push origin feature/new-dashboard
# 5. Create Pull Request on GitHub/GitLab
# 6. After review and approval, merge via PR# 7. Delete branch after merginggit checkout maingit pull origin maingit branch -d feature/new-dashboardHandling Merge Conflicts
# Pull latest changesgit pull origin main
# If conflicts occur# <<<<<<< HEAD# Your changes# =======# Their changes# >>>>>>> branch-name
# 1. Open conflicted files# 2. Resolve conflicts manually# 3. Remove conflict markers# 4. Test your changes
# Stage resolved filesgit add .
# Complete the mergegit commit -m "merge: resolve conflicts with main"Interactive Rebase
Clean up your commit history:
# Rebase last 3 commitsgit rebase -i HEAD~3
# In editor, you'll see:# pick abc123 feat: add feature A# pick def456 fix: typo# pick ghi789 feat: add feature B
# Change to:# pick abc123 feat: add feature A# fixup def456 fix: typo# pick ghi789 feat: add feature B
# Save and exit - commits will be combinedGit Hooks with Husky
Automate checks before commits:
npm install --save-dev husky lint-staged
# package.json{ "husky": { "hooks": { "pre-commit": "lint-staged", "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" } }, "lint-staged": { "*.{js,jsx,ts,tsx}": [ "eslint --fix", "prettier --write" ] }}Useful Git Commands
# View commit historygit log --oneline --graph --all
# Undo last commit (keep changes)git reset --soft HEAD~1
# Undo last commit (discard changes)git reset --hard HEAD~1
# Stash changesgit stashgit stash pop
# Cherry-pick specific commitgit cherry-pick <commit-hash>
# View changes in staginggit diff --staged
# Amend last commitgit commit --amend
# Show who changed each linegit blame <file>
# Search commitsgit log --grep="bug fix"
# Find which commit introduced a buggit bisect startgit bisect bad # Current version is badgit bisect good <commit-hash> # This version was goodPull Request Best Practices
Good PR:
- Small and focused (< 400 lines)
- Has descriptive title
- Includes description of changes
- Links to relevant issues
- Passes all CI checks
- Has screenshots for UI changes
- Requests specific reviewers
PR Template:
## DescriptionBrief description of changes
## Type of Change- [ ] Bug fix- [ ] New feature- [ ] Breaking change- [ ] Documentation update
## TestingHow to test these changes
## Screenshots(if applicable)
## Checklist- [ ] Code follows style guidelines- [ ] Self-reviewed code- [ ] Commented hard-to-understand areas- [ ] Updated documentation- [ ] No new warnings- [ ] Added tests- [ ] All tests passCommon Mistakes to Avoid
❌ Don't:
- Commit sensitive data (API keys, passwords)
- Make huge commits mixing multiple changes
- Use vague messages like "fix stuff" or "wip"
- Commit commented-out code
- Push directly to main/master
- Rewrite public history
✅ Do:
- Use .gitignore properly
- Make atomic commits (one logical change)
- Write descriptive messages
- Remove dead code
- Use feature branches
- Communicate with your team
Team Workflow Strategy
GitFlow
For projects with scheduled releases:
main- Productiondevelop- Integrationfeature/*- New featuresrelease/*- Release preparationhotfix/*- Emergency fixes
Trunk-Based Development
For continuous deployment:
- Everyone commits to
main - Short-lived feature branches (< 2 days)
- Feature flags for incomplete work
- Automated testing and deployment
Conclusion
Good Git practices are a team sport. Consistency matters more than perfection. Pick a workflow, document it, and stick to it.
Remember: Your commit history is a form of documentation. Make it count.