Learn Git

Distributed version control for tracking and sharing code changes

33 lessons · 6 hours · 4 levels · covers Git 2.51

Git records the history of a project as a series of snapshots and lets many people work on it in parallel through branches, merges and remotes. It is the version control system behind GitHub, GitLab and nearly every software team, so fluency in it is a baseline skill for any developer.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics10 lessons · 1 hour in total
2. PracticalUse it productively at work, with the usual idioms20 lessons · 3 hours in total
3. AdvancedThe less common features, performance and trade-offs28 lessons · 4 hours in total
4. ExpertInternals and the edge cases experts know33 lessons · 6 hours in total

The Git tree

GitWhat version control isWhat version controlisInstall and configure GitInstall and configureGitCreate or clone a repositoryCreate or clone arepositoryWorking tree, index and repositoryWorking tree, indexand repositoryStaging and committingStaging andcommittingInspecting changes: status and diffInspecting changes:status and diffReading history with git logReading history withgit logIgnoring files with .gitignoreIgnoring files with.gitignoreRemotes and remote-tracking branchesRemotes andremote-tracking br…Sharing work: push and pullSharing work: pushand pullBranchesBranchesMerging branchesMerging branchesResolving merge conflictsResolving mergeconflictsFetch vs pullFetch vs pullUndoing changes: restore and resetUndoing changes:restore and resetReverting commits safelyReverting commitssafelyStashing work in progressStashing work inprogressRebasing a branchRebasing a branchPull request workflowPull request workflowTagging releasesTagging releases
Essentials and Practical shown (20 of 33 lessons); the full list is below. Click a lesson to read it.

All 33 lessons, in order

Getting started

  1. What version control is · Essentials · 6 min

    Version control records every change to a set of files so you can compare, undo and share work safely.

  2. Install and configure Git · Essentials · 8 min

    Install Git, set your name and email, and pick sane defaults with git config.

  3. Create or clone a repository · Essentials · 6 min

    Start tracking a project with git init, or copy an existing repository with git clone.

  4. Working tree, index and repository · Essentials · 8 min

    Git moves changes through three places: the working tree you edit, the staging index, and the committed history.

Recording changes

  1. Staging and committing · Essentials · 10 min

    Use git add to stage changes and git commit to record them with a clear message.

  2. Inspecting changes: status and diff · Essentials · 8 min

    Read git status and git diff to know exactly what changed before you commit.

  3. Reading history with git log · Essentials · 9 min

    Browse, filter and search commit history with git log, git show and git blame.

  4. Ignoring files with .gitignore · Essentials · 7 min

    Keep build output, dependencies, secrets and editor junk out of the repository using .gitignore patterns.

Remotes and collaboration

  1. Remotes and remote-tracking branches · Essentials · 8 min

    A remote is a named URL of another copy of the repository; origin/main is your local record of its branch.

  2. Sharing work: push and pull · Essentials · 9 min

    Send your commits with git push and bring in others' commits with git pull.

  3. Fetch vs pull · Practical · 8 min

    git fetch downloads new commits without touching your branches; git pull fetches and then merges or rebases.

  4. Pull request workflow · Practical · 10 min

    Push a feature branch, open a pull request, get review and CI, then merge using your team's strategy.

  5. Tagging releases · Practical · 7 min

    Mark release points with annotated tags, push them, and use git describe to name builds.

Branching and merging

  1. Branches · Practical · 10 min

    A branch is a movable pointer to a commit; create, switch and delete them freely with git switch and git branch.

  2. Merging branches · Practical · 10 min

    Combine branches with git merge, and know when Git fast-forwards and when it creates a merge commit.

  3. Resolving merge conflicts · Practical · 12 min

    When both sides change the same lines, edit the conflict markers, git add the result and conclude the merge.

  4. Rebasing a branch · Practical · 12 min

    git rebase replays your commits on top of another branch, giving a linear history; never rebase shared commits.

Undoing things

  1. Undoing changes: restore and reset · Practical · 12 min

    Discard edits, unstage files and move branch pointers back with git restore and git reset.

  2. Reverting commits safely · Practical · 8 min

    git revert creates a new commit that undoes an earlier one, without rewriting shared history.

  3. Stashing work in progress · Practical · 7 min

    Save uncommitted changes aside with git stash, switch context, and reapply them later.

Rewriting and recovering history

  1. Interactive rebase · Advanced · 15 min

    Reorder, squash, reword, split or drop commits with git rebase -i before sharing them.

  2. Cherry-picking commits · Advanced · 8 min

    Apply the changes of specific commits onto your current branch with git cherry-pick.

  3. Reflog and recovering lost work · Advanced · 10 min

    The reflog records every position HEAD and your branches have had, so you can undo resets, bad rebases and deleted branches.

Power tools and workflows

  1. Finding bugs with git bisect · Advanced · 12 min

    Binary-search history with git bisect to find the exact commit that introduced a bug, manually or with a script.

  2. Multiple working trees · Advanced · 8 min

    Check out several branches at once in separate folders that share one repository with git worktree.

  3. Submodules · Advanced · 12 min

    Embed another repository at a pinned commit with git submodule, and keep it updated correctly.

  4. Git hooks · Advanced · 10 min

    Run scripts automatically at points like pre-commit, commit-msg and pre-push to enforce checks locally.

  5. Branching strategies · Advanced · 12 min

    Compare trunk-based development, GitHub flow and Git flow, and choose one that fits how you release.

Internals

  1. The object model: blobs, trees, commits · Expert · 15 min

    Git is a content-addressed store of four object types; commits point to trees, trees to blobs and subtrees.

  2. Refs, packfiles and garbage collection · Expert · 14 min

    How Git stores refs (loose, packed, reftable) and objects (loose zlib files, delta-compressed packfiles), and what gc does.

  3. How merges really work · Expert · 15 min

    The ort strategy, merge bases and criss-cross merges, strategy vs strategy options, rerere and merge-tree.

  4. Rewriting history at scale · Expert · 15 min

    Remove secrets or huge files from all of history with git filter-repo, and coordinate the force-push safely.

  5. Performance on big repositories · Expert · 15 min

    Make huge repos fast with partial and shallow clones, sparse checkout, commit-graph, fsmonitor and git maintenance.