Skip to content

Git Best Practices for Team Collaboration โ€‹

Introduction โ€‹

Beyond commands, effective collaboration depends on shared discipline. This guide enumerates pragmatic, enforceable practices that scale from small teams to large organizations.

Philosophy โ€‹

Golden principles:

  1. Clarity beats cleverness (history tells a story)
  2. Smaller changes ship faster
  3. Automation over manual policing
  4. Reproducibility = reliability

Branch Strategy Patterns โ€‹

StrategyDescriptionWhen to Use
Trunk-BasedShort-lived branches merged into main rapidlyFast-moving teams, CI maturity
GitHub FlowFeature branch โ†’ PR โ†’ merge โ†’ deploySaaS, continuous delivery
Git Flowdevelop, release, hotfix branchesVersioned products, slower cadence
Release TrainTime-boxed merges & releasesCoordinated multi-team releases

Pick the simplest that meets governance needs. Avoid over-engineering.

Commit Message Convention (Example) โ€‹

<type>(<scope>): <short summary>

<body>

<footer>

Types: feat, fix, docs, refactor, test, chore, perf. Use conventional commits to enable automated changelogs.

Enforce via Hooks / CI โ€‹

Examples:

bash
# .husky/commit-msg
#!/bin/sh
exec < /dev/tty
commitlint -E HUSKY_GIT_PARAMS

Pull Request Hygiene โ€‹

PracticeTarget
Max lines changed~400 (soft)
Review turnaround< 24h
All checks passRequired before merge
Linked issue present100%

Avoiding Long-Lived Branches โ€‹

Mitigation tactics:

  • Feature flags (merge incremental slices)
  • API compatibility layers
  • Dark launches / toggles
  • Early upstream rebases

Handling Hotfixes โ€‹

Workflow example:

bash
git checkout main
git pull
git checkout -b hotfix/critical-timezone
# fix, commit, test
git push origin hotfix/critical-timezone
# PR โ†’ merge โ†’ tag โ†’ deploy

Tagging Releases โ€‹

bash
git tag -a v2.3.0 -m "Release v2.3.0"
git push origin v2.3.0

Protecting Main Branch โ€‹

Controls to enable (platform settings):

  • Required status checks
  • Required reviews (โ‰ฅ1 or โ‰ฅ2)
  • Disallow force pushes
  • Linear history (optional)
  • Signed commits (if security sensitive)

Large File & Monorepo Guidance โ€‹

  • Use Git LFS for binaries
  • Adopt sparse checkout for huge repos
  • Define CODEOWNERS for critical paths

Standard .gitignore Baseline โ€‹

Keep it minimal + project-specific. Avoid committing generated artifacts.

Pre-Merge Checklist (Automated Where Possible) โ€‹

  • [ ] Tests pass
  • [ ] Linting/formatting clean
  • [ ] Security scan passes
  • [ ] No TODO left in changed code (or tracked issue)
  • [ ] Docs updated if behavior changed

Rewriting History Policy โ€‹

ActionAllowed?Notes
Rebase local featureYesBefore sharing
Force-push shared branchRareUse --force-with-lease
Rebase protected branchNoBreaks consumers
Squash on mergeConditionalIf preserving commit granularity not needed

Collaboration Anti-Patterns โ€‹

PatternProblemRemedy
Drive-by force pushesBreaks teammates' clonesRestrict permissions
Mixed concerns PRHard to reviewSplit by concern
Silent mergesLow visibilityRequire PR discussions
Working directly on mainRisk of broken productionEnforce branch protection

Documentation Anchors โ€‹

Maintain:

  • CONTRIBUTING.md
  • CODEOWNERS
  • Architecture decision records (ADR)
  • Onboarding cheat sheet

Metrics (Health Indicators) โ€‹

  • Mean time to merge (MTTM)
  • % PRs > 1,000 LOC (reduce)
  • Flaky test rate
  • Revert frequency post-merge

Summary โ€‹

Effective Git collaboration blends social agreements, automation, and disciplined workflows. Make the happy path the easy path.

Next Steps โ€‹

  • Hooks & automation (git-hooks-and-automation.md)
  • Undo strategies (git-reset-revert-and-checkout-explained.md)

Key Commands

bash
git tag -a <tag> -m "msg"
git push origin <tag>
git push origin --delete <branch>