Back to Blog

Git Workflow Best Practices for Teams

Minh Duy
Published on January 4, 2025
6 min read
Git Workflow Best Practices for Teams
DevOps
Git
Version Control
Team Collaboration
Best Practices

Git Workflow Best Practices for Teams

Effective Git workflows are crucial for team collaboration and maintaining code quality. This guide covers proven strategies that successful development teams use.

Popular Git Workflows

1. Feature Branch Workflow

# Create feature branch
git checkout -b feature/user-authentication
git push -u origin feature/user-authentication

# Work on feature
git add .
git commit -m "feat: add user login functionality"
git push

# Create pull request when ready
# After review and approval, merge to main

2. Gitflow Workflow

# Initialize gitflow
git flow init

# Start new feature
git flow feature start user-profile

# Finish feature
git flow feature finish user-profile

# Start release
git flow release start 1.2.0

# Finish release
git flow release finish 1.2.0

3. GitHub Flow

# Simple workflow for continuous deployment
git checkout main
git pull origin main
git checkout -b fix/login-bug
# Make changes
git commit -m "fix: resolve login validation issue"
git push origin fix/login-bug
# Create PR, review, merge

Commit Message Conventions

Conventional Commits

# Format: type(scope): description
feat(auth): add OAuth2 integration
fix(api): resolve user data validation
docs(readme): update installation instructions
style(css): improve button styling
refactor(utils): optimize date formatting
test(auth): add unit tests for login
chore(deps): update dependencies

Good vs Bad Commits

# ❌ Bad commits
git commit -m "fix"
git commit -m "update stuff"
git commit -m "working on feature"

# ✅ Good commits
git commit -m "fix: resolve null pointer exception in user service"
git commit -m "feat: add password reset functionality"
git commit -m "refactor: extract email validation to utility function"

Branch Protection and Code Review

Branch Protection Rules

# .github/branch-protection.yml
protection_rules:
  main:
    required_status_checks:
      - ci/tests
      - ci/lint
    enforce_admins: true
    required_pull_request_reviews:
      required_approving_review_count: 2
      dismiss_stale_reviews: true
    restrictions:
      users: []
      teams: ["core-team"]

Code Review Checklist

## Code Review Checklist

### Functionality
- [ ] Code does what it's supposed to do
- [ ] Edge cases are handled
- [ ] Error handling is appropriate

### Code Quality
- [ ] Code is readable and well-documented
- [ ] No code duplication
- [ ] Functions are small and focused
- [ ] Variable names are descriptive

### Testing
- [ ] Unit tests are included
- [ ] Tests cover edge cases
- [ ] All tests pass

### Security
- [ ] No sensitive data in code
- [ ] Input validation is present
- [ ] Authentication/authorization is correct

Merge Strategies

1. Merge Commit

git checkout main
git merge feature/user-auth
# Creates merge commit preserving branch history

2. Squash and Merge

git checkout main
git merge --squash feature/user-auth
git commit -m "feat: add user authentication system"
# Combines all commits into single commit

3. Rebase and Merge

git checkout feature/user-auth
git rebase main
git checkout main
git merge feature/user-auth
# Linear history without merge commits

Handling Conflicts

Merge Conflict Resolution

# When conflicts occur
git status
# Edit conflicted files
git add .
git commit -m "resolve: merge conflict in user service"

# Using merge tool
git mergetool

# Abort merge if needed
git merge --abort

Preventing Conflicts

# Keep branches up to date
git checkout main
git pull origin main
git checkout feature/my-feature
git rebase main

# Use smaller, focused commits
# Communicate with team about overlapping work
# Use feature flags for large changes

Git Hooks for Quality Control

Pre-commit Hook

#!/bin/sh
# .git/hooks/pre-commit

# Run linting
npm run lint
if [ $? -ne 0 ]; then
  echo "Linting failed. Please fix errors before committing."
  exit 1
fi

# Run tests
npm test
if [ $? -ne 0 ]; then
  echo "Tests failed. Please fix tests before committing."
  exit 1
fi

echo "Pre-commit checks passed!"

Commit Message Hook

#!/bin/sh
# .git/hooks/commit-msg

# Check commit message format
commit_regex='^(feat|fix|docs|style|refactor|test|chore)((.+))?: .{1,50}'

if ! grep -qE "$commit_regex" "$1"; then
    echo "Invalid commit message format!"
    echo "Format: type(scope): description"
    echo "Example: feat(auth): add user login"
    exit 1
fi

Advanced Git Techniques

Interactive Rebase

# Clean up commit history before merging
git rebase -i HEAD~3

# Options in interactive rebase:
# pick = use commit
# reword = use commit, but edit message
# edit = use commit, but stop for amending
# squash = use commit, but meld into previous commit
# fixup = like squash, but discard commit message
# drop = remove commit

Cherry Picking

# Apply specific commit to current branch
git cherry-pick abc123

# Cherry pick range of commits
git cherry-pick abc123..def456

# Cherry pick without committing
git cherry-pick --no-commit abc123

Bisect for Bug Hunting

# Find commit that introduced bug
git bisect start
git bisect bad HEAD
git bisect good v1.0.0

# Git will checkout commits for testing
# Mark each as good or bad
git bisect good  # or git bisect bad

# When found, reset
git bisect reset

Team Collaboration Tips

1. Communication

## Pull Request 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

2. Release Management

# Semantic versioning
git tag -a v1.2.0 -m "Release version 1.2.0"
git push origin v1.2.0

# Generate changelog
git log --oneline v1.1.0..v1.2.0

# Create release branch
git checkout -b release/1.2.0

3. Hotfix Process

# Emergency fix process
git checkout main
git checkout -b hotfix/critical-security-fix
# Make fix
git commit -m "fix: resolve critical security vulnerability"
# Fast-track review and merge
git checkout main
git merge hotfix/critical-security-fix
git tag -a v1.2.1 -m "Hotfix release 1.2.1"

Conclusion

Effective Git workflows require:

  1. Clear branching strategy that fits your team
  2. Consistent commit messages for better history
  3. Code review process for quality control
  4. Automated checks with hooks and CI/CD
  5. Good communication and documentation

Choose workflows that match your team size, deployment frequency, and project complexity.

Happy collaborating! 🚀

Related Posts