davidveksler@cheatsheets: ~/version-control

man version-control

Modern Version Control

Git, Mercurial & the hosting platforms that wrap them โ€” core concepts, the daily workflows, and how to choose. Every section expands with Details.

quick-ref # the 20% of Git you run all day

Everyday Git commands โ€” the highest-frequency lookups
CommandWhat it does
git clone <url>Copy a remote repo (+ full history) locally
git statusShow staged / unstaged / untracked changes
git add -pInteractively stage hunks into the index
git commit -m "โ€ฆ"Snapshot staged changes with a message
git switch -c feat/xCreate + move to a new branch (modern checkout -b)
git pull --rebaseFetch + replay your commits on top (linear history)
git push -u origin HEADPush current branch, set upstream tracking
git log --oneline --graphCompact visual history
git restore --staged <f>Unstage a file (keep working-tree edits)
git stash / git stash popShelve dirty changes, then reapply them

git help concepts

The mental model shared by every DVCS.

DVCS Philosophy

Distributed Version Control Systems (DVCS) like Git & Mercurial give every developer a full repository copy, enabling offline work, faster operations, and flexible workflows.

Details
Core Idea

Unlike older Centralized VCS (like SVN or CVS) where developers checked out files from a single central server, DVCS provides each user with a complete, independent copy (clone) of the entire repository history.

Key Advantages
  • Offline Work: Commit, branch, view history, and merge without needing a network connection.
  • Performance: Most operations (commit, branch, merge, diff) are local and thus significantly faster.
  • Redundancy: Every clone is effectively a full backup of the repository.
  • Workflow Flexibility: Enables complex branching strategies and collaboration models (e.g., pull requests).
  • Collaboration: Easily share changes between any two repositories, not just client-server.
Shift from Centralized

The move to DVCS addressed key limitations of CVCS, particularly around branching/merging complexity, performance bottlenecks, and reliance on network connectivity.

Repository (repo)

A collection of files and the history of their changes. In DVCS, you have a local copy and interact with remote copies (e.g., on GitHub).

Details
What it Contains

A repository stores all the files belonging to a project, plus the complete history of modifications made to those files over time. This history is typically stored as a series of commits.

Local vs. Remote
  • Local Repository: The full copy residing on your own computer. This is where you do your work: edit files, commit changes, create branches.
  • Remote Repository: A copy hosted on a server (e.g., GitHub, GitLab, a company server). Used for collaboration, backup, and sharing code. A common conventional name for the primary remote is origin.
Operations

You typically clone a remote repository to create your local copy, pull changes from a remote, and push your local changes to a remote. Fetch retrieves changes without merging.

Commit

A snapshot of your project's tracked files at a point in time, saved to history. Each commit has a unique ID (e.g., Git SHA-1 hash) and a message.

Details
Purpose

Commits are the fundamental building blocks of a project's history. They represent saved states that you can reference, revert to, or compare against.

Components
  • Snapshot: Records the state of all tracked files at that moment. Git stores this efficiently, only recording changes relative to previous commits.
  • Metadata: Author, committer, timestamp, and a unique identifier (a SHA-1 hash in Git).
  • Parent(s): Points to the preceding commit(s). A standard commit has one parent; a merge commit has two or more.
  • Commit Message: A description explaining the changes. Crucial for understanding history.
Workflow (Git)

Typically, you modify files, stage the changes you want to include (git add), and then commit them with a message (git commit).

Branch

An independent line of development. Branches let you work on features or fixes without affecting the main codebase (e.g., main).

Details
Concept

Think of a branch as a movable pointer to a specific commit. When you create a branch, you create a new pointer. As you make commits, the pointer moves forward with your changes.

Why Use Branches?
  • Isolation: Develop features, experiment, or fix bugs without destabilizing the main line.
  • Parallel Development: Multiple people work on different features simultaneously.
  • Organization: Keep related changes grouped (e.g., a feature/user-login branch).
  • Workflow Enablement: Basis for Gitflow, GitHub Flow, etc., often involving Pull/Merge Requests.
Common Operations (Git)

Create (git branch feature-x), switch (git switch feature-x), commit, then merge back.

Merge

The process of combining changes from different branches back into one, reconciling divergent histories.

Details
Goal

To integrate work done on a separate branch (e.g., a feature branch) into another (e.g., main).

How it Works
  • Fast-Forward Merge: If the target branch hasn't diverged, its pointer simply moves forward to the source's latest commit. Linear history.
  • Three-Way Merge (Merge Commit): If both diverged, the VCS finds a common ancestor and combines changes since then, creating a new merge commit with two parents.
Merge Conflicts

Occur when both branches modified the same part of the same file differently. The VCS can't auto-decide, requiring manual resolution before completing the merge.

Alternatives

Rebasing (git rebase) rewrites one branch's history on top of another for a linear result โ€” but avoid it on already-shared branches.

ls /usr/bin/{git,hg}

The two mainstream distributed systems โ€” and one to watch.

Git git-scm.com

The dominant DVCS: speed, flexibility, powerful branching/merging, and a vast ecosystem. Features a staging area (index) for crafting commits. git-scm.com

Details
Philosophy & Core

Designed by Linus Torvalds for Linux kernel development. Focuses on performance, data integrity (content-addressable storage via SHA-1 hashes), and robust non-linear workflows.

Key Features
  • Staging Area (Index): Stage parts of files to craft precise, logical commits.
  • Branching Model: Extremely lightweight, fast branching and merging.
  • Distributed: Every clone is a full repository.
  • Performance: Most operations are local and fast.
  • Flexibility: Supports Gitflow, GitHub Flow, etc.; rebase allows history editing (use with care on shared branches).
  • Large Files: Handled via Git LFS.
Common Commands

clone, add, commit, status, log, branch, switch, merge, rebase, pull, push, fetch.

Tradeoffs
  • Steeper learning curve for staging area, reset, rebase.
  • CLI can feel complex due to many options.
  • Default binary handling isn't ideal without LFS.
Use Cases

Software of all sizes, open source, infrastructure as code, docs โ€” anywhere you track changes to text files.

Mercurial (hg)

A DVCS focused on simplicity and ease of use. Often felt more intuitive than Git initially, but far less used today. mercurial-scm.org

Details
Philosophy & Core

Designed with user-friendliness and interface consistency as priorities โ€” powerful DVCS features with a simpler command set than Git's defaults.

Key Features
  • Simpler Interface: No staging area by default; hg commit commits all tracked changes.
  • Branching Concepts: Named branches, bookmarks, anonymous heads. Named branches are permanent parts of history โ€” powerful but confusing.
  • Extensibility: Extensions add rebase, histedit, largefiles.
  • Performance: Generally excellent, comparable to Git.
  • Windows Support: Historically strong.
Common Commands

hg clone, hg add, hg commit, hg status, hg log, hg branch, hg update, hg merge, hg pull, hg push, hg heads.

Tradeoffs
  • Far smaller community and ecosystem than Git.
  • No major hosting: Bitbucket removed all Hg repos in July 2020 (deleted by March 2022); GitHub never supported it; GitLab needs workarounds.
  • Multiple branching models can confuse.
Use Cases

Legacy projects and a few large orgs. Meta used it internally before building its own Sapling system.

โ–ฒ Watch โ€” Jujutsu (jj): a Git-compatible VCS gaining real traction (used internally at Google; ~30k GitHub stars; v0.43.0 as of July 2026). Simpler mental model โ€” no staging area, first-class conflict handling, anonymous branches โ€” while reading/writing Git repos natively for gradual adoption. Still pre-1.0 with gaps (limited IDE integration, no submodule support), but production-ready for individuals and small teams. jj-vcs/jj

git remote -v # where repos live

The four platforms that host Git and wrap it in collaboration + CI/CD.

GitHub

The most popular Git host. Strong focus on collaboration, open source, and developer community. github.com

Details
Core Offerings

Git hosting, Pull Requests, issues, Projects (boards/tables), CI/CD (GitHub Actions), Packages, wikis, security scanning (Dependabot, Code Scanning).

Key Features
  • Pull Requests: Central code-review + merge mechanism.
  • Actions: Powerful integrated CI/CD in YAML.
  • Community: The de facto hub for open source.
  • Codespaces: Cloud dev environments.
  • Copilot: AI pair programmer.
Strengths
  • Massive user base / network effect.
  • Excellent UI/UX; flexible Actions CI/CD.
  • Generous free tier; strong security features.
Tradeoffs
  • Owned by Microsoft (a concern for some).
  • CI/CD limits on lower tiers can bite large private projects.
  • Built-in project management less deep than Jira/Azure Boards.

GitLab

An integrated DevOps platform around Git hosting โ€” planning to monitoring. Self-hosting available. gitlab.com

Details
Core Offerings

Git hosting, Merge Requests, issues + boards, integrated CI/CD, container registry, extensive security scanning (SAST, DAST, dependency, secrets), monitoring, Epics/Roadmaps, wiki.

Key Features
  • Single Application: The whole DevOps lifecycle in one platform.
  • GitLab CI/CD: Highly integrated via .gitlab-ci.yml; shared or self-hosted runners.
  • Self-Managed: Install on your own infra (open-source CE, paid EE).
  • DevSecOps: Security scanning throughout MRs.
  • Auto DevOps: Opinionated build/test/deploy/monitor pipeline.
Strengths
  • All-in-one simplifies tooling; mature built-in CI/CD.
  • Strong integrated security; viable self-hosting (CE).
Tradeoffs
  • Feature volume can overwhelm the UI.
  • UX sometimes seen as less polished than GitHub.
  • Self-hosting adds operational overhead.

Bitbucket Cloud

Atlassian's Git host, known for deep Jira and Trello integration. (Mercurial support removed.) bitbucket.org

Details
Core Offerings

Git hosting (Mercurial fully removed July 2020; Hg repos deleted by March 2022), Pull Requests, integrated CI/CD (Pipelines), code search, deep Jira/Trello integration.

Key Features
  • Atlassian Ecosystem: Seamless Bitbucket โ†” Jira flow is the main draw.
  • Pipelines: CI/CD via bitbucket-pipelines.yml.
  • Code Insights: Surfaces scan/test results in PRs.
  • Free Tier: Free private repos for small teams (up to 5 users).
Strengths
  • Unbeatable if invested in Atlassian (especially Jira).
  • Competitive pricing; clean, simple UI.
Tradeoffs
  • Smaller community than GitHub/GitLab.
  • Pipelines less flexible for very complex scenarios.
  • Fewer built-ins than GitLab; Mercurial support is gone.

Azure DevOps

Microsoft's DevOps suite, including Azure Repos for Git. Strong Azure and Entra ID integration. azure.microsoft.com

Details
Core Offerings (Suite)

Azure Repos (Git + legacy TFVC), Azure Pipelines (CI/CD), Azure Boards (Agile planning), Azure Test Plans, Azure Artifacts (packages).

Key Features
  • Integrated Suite: Tools across the lifecycle; usable independently.
  • Azure Repos: Unlimited private Git repos; also supports TFVC.
  • Azure Pipelines: Powerful CI/CD, deploy anywhere (Azure/AWS/GCP/on-prem), YAML or visual.
  • Azure Boards: Mature, customizable planning (Jira-comparable).
  • Enterprise: Granular permissions, branch policies, auditing, Entra ID.
Strengths
  • Excellent Azure integration; mature Pipelines & Boards.
  • Strong governance; generous free tier for small teams / OSS.
Tradeoffs
  • UI can feel dated / fragmented across services.
  • Less prominent in open source; feels Microsoft-centric.

git flow --help

How teams actually turn commits into shipped software.

Branching Strategies

Patterns for using branches to manage development, releases, and fixes โ€” Gitflow, GitHub Flow, Trunk-Based Development.

Explore
Common Strategies
  • Gitflow: Dedicated main, develop, feature/*, release/*, hotfix/* branches. Good for scheduled releases; can be complex.
  • GitHub Flow: main always deployable; feature branches merge via PR and deploy immediately.
  • GitLab Flow: Adds environment/release branches, bridging continuous deployment and versioned releases.
  • Trunk-Based Development: Very short-lived branches or direct commits to main; relies on feature flags, automated testing, robust CI/CD.

The best strategy depends on team size, release cadence, deployment practices, and discipline.

Pull / Merge Requests

The core collaboration feature to propose changes between branches โ€” enabling code review, discussion, automated checks, and controlled merging.

Explore
Process Overview
  1. Create a feature branch, commit locally, push to the remote.
  2. Open a PR (GitHub/Bitbucket/Azure) or MR (GitLab) targeting the integration branch.
  3. Reviewers examine the diff, comment, request changes.
  4. CI/CD checks (builds, tests, linters, scans) report status to the PR/MR.
  5. Discussion + updates continue until approved and green.
  6. An authorized user merges into the target branch, closing the PR/MR.
Benefits
  • Code Quality: Peer review catches bugs, enforces standards.
  • Knowledge Sharing: Team learns context across the project.
  • Gatekeeping: Ensures changes pass checks before critical branches.
  • Discussion Record: Durable history of decisions.

CI/CD Integration

VCS events (pushes, PRs) trigger Continuous Integration (auto build/test) and Continuous Delivery/Deployment pipelines.

Explore
Continuous Integration (CI)

Frequently merge working copies to a shared mainline; each merge triggers an automated build + tests.

  • Trigger: On push, or on PR/MR updates to key branches.
  • Goal: Detect integration/build/test failures early.
  • Benefit: Fast feedback; less "merge hell."
Continuous Delivery / Deployment (CD)
  • Delivery: Auto build/test/prepare; final production deploy needs manual approval.
  • Deployment: Every validated change auto-deploys to production โ€” requires high test confidence.
  • Trigger: After CI succeeds on specific branches or on tag creation.
Role of VCS & Platforms

VCS events act as pipeline triggers. Platforms provide integrated CI/CD (Actions, GitLab CI, Pipelines) or integrate with Jenkins/CircleCI. Pipeline definitions live as YAML in the repo.

which vcs && which host

Decision guidance โ€” start here if you're setting up a new project.

System & Platform Selection Factors

Default to Git for its ubiquity. Pick a hosting platform by ecosystem integration (Atlassian, Microsoft, open source), feature depth (CI/CD, security, planning), community vs. enterprise focus, self-hosting needs, and pricing.

Key Factors
Choosing the VCS (Git vs. Mercurial vs. jj)
  • Default: Git โ€” the overwhelming standard; maximum compatibility, community, talent, tooling, platform choice.
  • Mercurial only if: you maintain large legacy Hg projects. No major host supports new Hg repos; former heavy users (Meta) moved on.
  • Watch jj: Git-compatible, simpler model, gradual adoption alongside Git. Pre-1.0 but usable for individuals/small teams.
Choosing the Host
  • Ecosystem: Bitbucket โ†’ Atlassian/Jira; Azure DevOps โ†’ Microsoft/Entra ID; GitHub โ†’ open source + Marketplace; GitLab โ†’ be the whole ecosystem.
  • Feature depth: GitLab most comprehensive built-in; Azure mature Pipelines/Boards; GitHub leading Actions + community + security add-ons; Bitbucket core hosting + Jira.
  • Hosting model: GitLab strong SaaS + self-managed; GitHub mainly SaaS (+ Enterprise Server); Azure mainly SaaS; Bitbucket Cloud + Data Center.
  • Community vs. enterprise: GitHub leads open source; GitLab strong both; Azure enterprise-leaning; Bitbucket SMB + Atlassian shops.
  • Pricing: Compare free-tier limits (users, private repos, CI/CD minutes, storage) against your size and usage.
  • UI/UX: Subjective โ€” trial the free tiers with your team.
Standardization vs. Polyglot

You can mix platforms, but most orgs standardize on one for consistency, billing, user management, and unified workflow โ€” integrating best-of-breed tools where needed.