Git and GitHub Best Practices: Branching Strategies and Workflows

Git and GitHub Best Practices: Branching Strategies and Workflows

Introduction

[Explain version control as foundation for collaboration, CI/CD, and release management; introduce common workflow patterns.]

Prerequisites

  • Git installed locally
  • GitHub account
  • Basic Git commands familiarity

Branching Strategies Comparison

Strategy Use Case Complexity
Trunk-Based High-frequency CI/CD Low
GitFlow Structured release cycles High
GitHub Flow Continuous deployment Medium
Feature Branch Isolated development Low

Step-by-Step Guide

Step 1: Choose Branching Model

GitHub Flow (Recommended for Web Apps)

  • main branch always deployable
  • Feature branches for changes
  • Pull requests for review
  • Merge to main triggers deployment

Step 2: Feature Branch Workflow

# Create feature branch
git checkout -b feature/add-user-profile

# Make changes and commit
git add .
git commit -m "feat: add user profile page"

# Push to remote
git push -u origin feature/add-user-profile

Step 3: Pull Request Best Practices

PR Template Example:

## Description
[Summary of changes]

## Type of Change
- [ ] Bug fix
- [ ] New feature
- [ ] Breaking change

## Checklist
- [ ] Tests added/updated
- [ ] Documentation updated
- [ ] No merge conflicts

Step 4: Commit Message Conventions

Conventional Commits:

feat: add email notification service
fix: resolve null reference in auth middleware
docs: update API documentation
refactor: simplify user repository
test: add integration tests for orders

Step 5: Code Review Process

  1. Automated checks (CI build, lint, tests)
  2. Peer review (at least 1 approval)
  3. Resolve comments
  4. Merge strategy (squash, merge, rebase)

Step 6: Merge Strategies

Squash Merge (Clean History):

git merge --squash feature/add-user-profile
git commit -m "feat: add user profile page"

Rebase (Linear History):

git checkout feature/add-user-profile
git rebase main
git checkout main
git merge feature/add-user-profile

Step 7: Protected Branches

GitHub Settings:

  • Require pull request reviews (1-2 approvers)
  • Require status checks to pass
  • Enforce linear history
  • Restrict force pushes

Step 8: Git Hooks for Quality

Pre-commit Hook (.git/hooks/pre-commit):

#!/bin/sh
npm run lint
npm test

Advanced Git Techniques

Interactive Rebase

git rebase -i HEAD~3
# Edit, squash, reorder commits

Cherry-Pick Commits

git cherry-pick abc123def

Stash Changes

git stash save "WIP: refactoring"
git stash pop

GitHub Actions Integration

name: PR Validation
on: pull_request
jobs:
  validate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm ci
      - run: npm run lint
      - run: npm test

Common Anti-Patterns to Avoid

  • Long-lived feature branches (merge frequently)
  • Force push to shared branches
  • Committing secrets or credentials
  • Skipping code reviews
  • Ambiguous commit messages

Troubleshooting

Issue: Merge conflicts
Solution: Rebase on target branch frequently; resolve conflicts locally

Issue: Accidental commit to main
Solution: Reset to previous commit; force push (if no collaborators affected)

Issue: Large binary files in repo
Solution: Use Git LFS; clean history with git-filter-repo

Best Practices Summary

  • Keep commits atomic and focused
  • Write descriptive commit messages
  • Review your own PR before requesting reviews
  • Automate checks with CI/CD
  • Delete merged branches promptly

Key Takeaways

  • Consistent branching strategy improves collaboration.
  • Pull requests enable code quality gates.
  • Conventional commits enhance changelog generation.
  • Protected branches prevent accidental mistakes.

Next Steps

  • Implement Git hooks for automated quality checks
  • Set up branch protection rules
  • Create PR templates for your team

Additional Resources


Which workflow will standardize your team's collaboration?