Git Best Practices: A Comprehensive Guide
Drawing on two decades of version‑control experience, this guide outlines essential Git best practices—including clear branch strategies, squash‑and‑rebase workflows, fetch‑plus‑rebase instead of pull, atomic commits, proper LFS configuration, and tool recommendations—while clarifying what Git is versus surrounding services and addressing large‑repo limitations.
This article presents Git best practices based on over 20 years of version control experience and more than 10 years of Git-specific expertise.
What Git Is: Git is a distributed version control system that tracks directory modifications with a non-linear workflow. Each Git directory on each machine is a complete repository containing full history and version tracking capabilities. Unlike master-slave systems, there is no central server role—all repositories are equal endpoints.
What Git Is Not: GitHub, GitLab, Fork, and Merge/Pull Requests are not part of Git itself—they are services and workflows built around Git. Understanding this distinction is crucial for proper Git usage.
Tool Selection: The author recommends TortoiseGit for Windows as the best GUI tool, as it integrates with Windows Explorer and provides a visual branch graph viewer essential for understanding Git's non-linear workflow.
Branch Strategy: The article covers main/develop branches for stability, team-specific develop branches for large projects, and short-lived feature branches for individual features. Feature branches should be created frequently, worked on, merged, and deleted.
Merge vs Rebase: The author explains these as two dimensions: merge preserves branch structure while rebase reapplies commits. For feature branches, squash + rebase is recommended as the most efficient approach. The workflow should be: commit/amend during development, squash to one commit before MR, continue amend during MR iteration, and rebase after MR approval.
Conflict Resolution: Best practice is to rebase feature branch onto target branch, resolve conflicts, then force push—avoiding creating new branches for conflict resolution.
Pull vs Fetch: Instead of pull, use fetch + rebase to avoid unnecessary merges. Manual fetch gets all branches, allowing better control.
Commit Principles: Commits should be small and complete (atomic). Avoid combining multiple features in one commit or having incomplete commits that require other commits to be useful.
LFS Usage: Git LFS handles large binary files but requires explicit configuration in .gitattributes before adding large files. The author provides tips on proper LFS usage and avoiding abuse.
Git's Limitations: Git lacks built-in branch permission management and can have performance issues with massive repositories. Solutions like partial clone, sparse checkout, and tools like Microsoft's VFSForGit (now Scalar) address these issues for large projects.
Tencent Cloud Developer
Official Tencent Cloud community account that brings together developers, shares practical tech insights, and fosters an influential tech exchange community.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.