Git Best Practices: Branch Strategies and Team Workflows
DevOps11 min read

Git Best Practices: Branch Strategies and Team Workflows

Master Git workflows, branch strategies, and commit conventions. Learn Gitflow, GitHub Flow, Trunk Based Development, and team collaboration best practices.

Taha Kocal

Taha Kocal

Full Stack Developer

Nov 20, 2024
#Git#Version Control#DevOps#Collaboration#Best Practices

Git best practices are the conventions - branch strategies, commit message standards, and review workflows - that keep a shared repository clean, traceable, and safe to deploy from. The three dominant branch strategies each fit a different team: Gitflow suits large teams shipping versioned releases with its develop, release, and hotfix branches; GitHub Flow simplifies this to a single main branch plus short-lived feature branches merged through pull requests; and Trunk Based Development pushes small commits to main multiple times a day behind feature flags. On top of a strategy, the Conventional Commits specification standardizes messages with prefixes like feat, fix, and refactor, which enables automated changelogs and semantic versioning. This guide covers all three workflows, commit conventions, power commands such as interactive rebase and bisect, Git hooks with Husky, and the pull request habits that keep team collaboration fast.

Which Branch Strategy Should You Choose?

1. Gitflow

Gitflow is ideal for large teams and versioned releases. It provides a structured approach to managing features, releases, and hotfixes.

Branch types in Gitflow:

  • main: Production-ready code
  • develop: Integration branch for features
  • feature/*: New feature development
  • release/*: Release preparation and testing
  • hotfix/*: Emergency production fixes
bash
# Start a feature
git checkout develop
git checkout -b feature/user-authentication

# Finish a feature
git checkout develop
git merge --no-ff feature/user-authentication
git branch -d feature/user-authentication

# Start a release
git checkout develop
git checkout -b release/v1.0.0

# Finish a release
git checkout main
git merge --no-ff release/v1.0.0
git tag -a v1.0.0 -m "Version 1.0.0"
git checkout develop
git merge --no-ff release/v1.0.0

2. GitHub Flow

GitHub Flow is a simpler alternative for CI/CD-focused teams. It uses a single main branch with feature branches.

GitHub Flow steps:

  • Create a branch from main
  • Make commits with meaningful messages
  • Open a Pull Request
  • Conduct code review
  • Merge to main and deploy
bash
# New feature
git checkout main
git pull origin main
git checkout -b add-user-profile

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

# Push and create PR
git push -u origin add-user-profile
# Open PR on GitHub

3. Trunk Based Development

Trunk Based Development is optimal for continuous integration with small, frequent commits directly to the main branch.

Key characteristics:

  • Small, frequent commits (multiple per day)
  • Feature flags hide incomplete features
  • Short-lived branches (max 1-2 days)
  • Continuous integration to main

How Should You Write Commit Messages?

Good commit messages are essential for project maintainability. The Conventional Commits specification provides a standardized format.

Commit types:

  • feat: New feature
  • fix: Bug fix
  • docs: Documentation changes
  • style: Formatting (no code change)
  • refactor: Code refactoring
  • perf: Performance improvement
  • test: Adding/fixing tests
  • chore: Build, config changes
bash
# New feature
git commit -m "feat(auth): add OAuth2 login support"

# Bug fix with description
git commit -m "fix(cart): resolve quantity update issue

The cart was not updating when quantity changed to 0.
This commit adds proper validation and removes the item.

Closes #123"

# Breaking change
git commit -m "feat(api)!: change user endpoint response format

BREAKING CHANGE: User endpoint now returns nested address object"

Useful Git Commands

Interactive Rebase

bash
# Edit last 3 commits
git rebase -i HEAD~3

# Squash commits (in interactive mode)
pick abc1234 First commit
squash def5678 Second commit
squash ghi9012 Third commit

Stash for Temporary Changes

bash
# Save changes temporarily
git stash

# Save with descriptive message
git stash push -m "WIP: user profile feature"

# List all stashes
git stash list

# Apply and remove latest stash
git stash pop

# Apply specific stash
git stash apply stash@{2}

Cherry Pick

bash
# Pick a single commit from another branch
git cherry-pick abc1234

# Pick multiple commits
git cherry-pick abc1234 def5678

# Continue after resolving conflicts
git cherry-pick --continue

Bisect for Bug Hunting

bash
# Find which commit introduced a bug
git bisect start
git bisect bad HEAD
git bisect good v1.0.0

# Git checks out commits - test and report
git bisect good  # or git bisect bad

# Finish when found
git bisect reset

Git Hooks with Husky

bash
npm install husky lint-staged -D
npx husky install
json
// package.json
{
  "lint-staged": {
    "*.{js,ts,tsx}": [
      "eslint --fix",
      "prettier --write"
    ]
  }
}

Git Aliases

bash
# ~/.gitconfig
[alias]
  co = checkout
  br = branch
  ci = commit
  st = status
  lg = log --oneline --graph --decorate
  ll = log --pretty=format:'%C(yellow)%h%Creset %s %C(cyan)<%an>%Creset'
  new = checkout -b
  del = branch -d
  unstage = reset HEAD --
  undo = reset --soft HEAD~1

How Do Teams Collaborate Effectively with Git?

Pull Request guidelines:

  • Keep PRs small (max 400 lines of changes)
  • Write descriptive title and description
  • Link to related issues
  • Add screenshots/videos for UI changes
  • Request reviews from relevant team members

Code Review checklist:

  • DRY principle applied
  • Functions have single responsibility
  • Error handling implemented
  • Unit tests written
  • No N+1 queries or performance issues
  • Input validation present

When used correctly, Git can significantly boost team productivity. By applying these practices, you achieve cleaner history, fewer conflicts, and faster deployments.

Whichever workflow you choose, consistency across your team is key. Start with these best practices and adapt them to your team's specific needs.

Share this article

Related Articles