🔧 Git

← Back to Cheatsheets

A quick-reference for Git version control. Git tracks changes as a directed acyclic graph of commits; each commit is a snapshot, not a diff. The working tree, staging area (index), and repository are the three layers you move changes through.

Resources

git-scm.com — Reference Pro Git (free book) Oh Shit, Git! — recovery guide Confusing git terminology

Basics

The everyday workflow: initialise or clone a repo, stage changes, commit, and repeat. Stage only what belongs in a commit — git add -p lets you stage hunks interactively.

Branching & Merging

Branches are just pointers to commits — they're cheap to create. Prefer git switch and git restore over git checkout for clarity (available since Git 2.23).

Remote

Remotes are named references to other repositories. origin is the conventional name for the repo you cloned from.

History & Diff

Navigating history. git log --oneline --graph gives a compact visual overview of the branch topology.

Undo

Three distinct tools depending on what you want to preserve. revert is the safe option for shared branches — it creates a new commit rather than rewriting history.

Stash

Stash saves uncommitted changes to a stack so you can switch context and come back later.

Worktree

Worktrees let you check out multiple branches simultaneously, each in its own directory, all linked to the same repository. Useful for working on a feature while fixing a bug on main without stashing or switching branches.

Config & Aliases

Git config is layered: --system (all users), --global (current user), --local (this repo). Local overrides global overrides system.