programmingconcepts

§ 8.2 · Tools

Git

A version control system that stores your project as a chain of snapshots, with branches as cheap, movable labels.

Git is a distributed version control system. Each commit records a snapshot of every file, a message, and a link to its parent commit, so the history forms a graph. Branches are just named pointers to commits, which makes them cheap to create. Every clone holds the full history, so most work happens offline and syncs later.

Created
2005, by Linus Torvalds
Stores
Snapshots, not diffs
A branch is
A pointer to one commit
Objects named by
Hash of their contents

Three places your files live

Most Git confusion comes from not knowing which of three places a change is in.

Place What it is Moves on with
Working tree The files you edit git add
Staging area (index) The draft of your next commit git commit
Repository Every commit ever made, in .git/ git push to share

git status tells you what’s different between each pair. Staging exists so you can commit part of your changes: fix two bugs, then commit them separately.

git add parser.py          # stage one file
git commit -m "Fix empty input in parser"
git log --oneline --graph  # see the history as a graph

Commits are snapshots

A commit doesn’t store “what changed”. It stores the full state of every file, plus the author, the message, and the ID of its parent commit. Git avoids wasting space by storing each unique file content once, under the hash of that content. If a file didn’t change, the new snapshot points at the same stored copy.

Because each commit points at its parent, history is a chain. A merge commit has two parents, so the chain becomes a graph.

Branches are pointers

A branch is a tiny file holding one commit ID. Making a branch copies 41 bytes; it doesn’t copy your code. When you commit, the current branch’s pointer moves forward to the new commit. HEAD is the pointer to whichever branch you’re on.

git switch -c fix-login    # new pointer at the current commit, and move to it
git commit -am "Check token expiry"
git switch main            # move HEAD back; files change to match main

Merge vs rebase

Two branches have diverged and you want to combine them.

  • Merge creates a new commit with two parents. History shows exactly what happened, including the fork.
  • Rebase replays your commits one by one on top of the other branch, creating new commits with new IDs. History becomes a straight line.

The rule of thumb: rebase your own unshared work to keep it tidy, merge anything other people already have. Rebasing shared commits rewrites history under someone else’s feet.

Common mistakes

  1. Committing secrets. Deleting a password in a later commit leaves it in history. Rotate the secret, and add files like .env to .gitignore before the first commit.
  2. Panicking after a bad reset. Git rarely deletes commits right away. git reflog lists where HEAD has been, so you can get back to them.
  3. Giant mixed commits. Small commits with clear messages make git bisect and code review work. One change, one commit.
  4. Force-pushing a shared branch. Use git push --force-with-lease, which refuses if someone else pushed in the meantime.

Check yourself

Check yourself

You edited app.js and ran git add app.js, but haven't committed. Where is the change?

Check yourself

What does creating a new branch actually copy?

Sources

  • Scott Chacon and Ben Straub, Pro Git, 2nd edition (2014), git-scm.com/book. Chapter 10, “Git Internals.”
  • The Git documentation, git-scm.com/docs.

Newsletter

One new concept each week, by email.

Signup opens soon.