Git Workflow Best Practices
Effective Git workflows are essential for team collaboration and maintaining code quality. Here are proven strategies and best practices.
Popular Git Workflows
1. Git Flow
Best for: Projects with scheduled releases
# Main branches
main # Production-ready code
develop # Integration branch
# Supporting branches
feature/* # New features
release/* # Release preparation
hotfix/* # Emergency fixes
2. GitHub Flow
Best for: Continuous deployment
main # Always deployable
feature/* # Short-lived feature branches
3. GitLab Flow
Best for: Different environments
main # Latest development
pre-production # Testing environment
production # Production environment
Branching Best Practices
Branch Naming Conventions
# Good examples
feature/user-authentication
bugfix/header-styling-issue
hotfix/security-vulnerability
refactor/database-queries
docs/api-documentation
# Bad examples
fix
new-feature
john-branch
temp
Branch Lifecycle
# Create and switch to new branch
git checkout -b feature/new-login
# Work on feature
git add .
git commit -m "Add login form validation"
# Push to remote
git push -u origin feature/new-login
# Create pull request
# After review and approval, merge to main
# Delete feature branch
git branch -d feature/new-login
git push origin --delete feature/new-login
Commit Best Practices
Commit Message Format
<type>(<scope>): <subject>
<body>
<footer>
Commit Types
feat: New featurefix: Bug fixdocs: Documentation changesstyle: Code style changesrefactor: Code refactoringtest: Adding or updating testschore: Build process or auxiliary tool changes
Examples
feat(auth): add two-factor authentication
Implement TOTP-based 2FA for enhanced security.
Users can now enable 2FA in their profile settings.
Closes #123
Code Review Guidelines
Before Creating PR
- Code is tested and working
- Tests are passing
- Documentation is updated
- Branch is up to date with main
- Commits are clean and meaningful
PR Template
## Description
Brief description of changes
## Type of Change
- [ ] Bug fix
- [ ] New feature
- [ ] Breaking change
- [ ] Documentation update
## Testing
- [ ] Unit tests pass
- [ ] Integration tests pass
- [ ] Manual testing completed
## Checklist
- [ ] Code follows style guidelines
- [ ] Self-review completed
- [ ] Documentation updated
Useful Git Commands
Cleanup Commands
# Remove merged branches
git branch --merged | grep -v "\*\|main\|develop" | xargs -n 1 git branch -d
# Prune remote branches
git remote prune origin
# Interactive rebase to squash commits
git rebase -i HEAD~3
Undoing Changes
# Undo last commit (keep changes)
git reset HEAD~1
# Undo last commit (discard changes)
git reset --hard HEAD~1
# Revert a commit
git revert <commit-hash>
Stashing
# Stash current changes
git stash push -m "Work in progress on feature X"
# List stashes
git stash list
# Apply specific stash
git stash apply stash@{0}
Merge Strategies
Merge Commit
- Preserves branch history
- Creates merge commits
- Good for feature branches
Squash and Merge
- Combines all commits into one
- Cleaner linear history
- Loses individual commit history
Rebase and Merge
- Linear history without merge commits
- Replays commits on target branch
- Requires force push if shared
Team Guidelines
Repository Rules
- Protected main branch - require PR reviews
- Status checks - CI/CD must pass
- Linear history - prefer rebase over merge
- Signed commits - for security
- Branch policies - auto-delete after merge
Communication
- Use meaningful commit messages
- Reference issues in commits
- Tag reviewers appropriately
- Respond to review feedback promptly
- Keep PRs small and focused
Tools and Integrations
Helpful Tools
- GitKraken - Visual Git client
- Sourcetree - Free Git GUI
- GitHub CLI - Command-line interface
- Conventional Commits - Commit message specification
- Husky - Git hooks for quality checks
IDE Integrations
Most modern IDEs have excellent Git integration:
- VS Code Git extension
- IntelliJ IDEA VCS
- Sublime Merge
- Vim Fugitive
Remember: The best workflow is one your team actually follows consistently!