Git Internals
Git is a small database of snapshots with a few pointers into it. Once you can see the objects and the pointers, commit, branch, merge, rebase and reset stop being magic and start being obvious.
- Beginner
- 4 units, 12 lessons, 155 exercises
- Shell
- Builds on having used git a little
Unit 01 · 3 lessons
What a commit is
See a commit as a full snapshot stored in a small database
- 01Snapshots, not diffs
A commit is the whole project plus a parent
- 02Three trees
Working directory, index and HEAD
- BOSSInside .git
Blobs, trees and commits, named by their content
Read the unit guide
Git is a small database of snapshots. Every commit records the whole project as it was at that moment, plus a pointer to the commit before it (its parent), an author and a message. Diffs are never stored: git works them out by comparing two snapshots when you ask.
Your project lives in three places at once. The working directory is the files you edit. The index (staging area) is a full draft of the next snapshot. HEAD is the last commit. git add copies a file from the working directory into the index, and git commit turns the index into a commit.
Read the gaps between the three with git diff (working directory against index), git diff --staged (index against HEAD) and git status --short, where the left column is staged and the right column is not.
Under the hood there are three kinds of object. A blob is the content of one file, with no name. A tree is a folder: names pointing at blobs and other trees. A commit points at one tree and at its parents. git cat-file -p prints any of them.
Every object is named by the hash of its own content. Identical files are therefore stored once, an unchanged file costs nothing in the next commit, and nobody can alter old history quietly: changing anything changes its hash, and the hash of every commit after it.
Unit 02 · 3 lessons
Branches are pointers
Read branch, merge and rebase as moves of a few small pointers
- 01Refs and HEAD
A branch is forty characters in a file
- 02Merging
Fast-forwards, merge commits and conflicts
- BOSSMerge or rebase
Replay commits on a new base, and know when not to
Read the unit guide
A branch is a file that holds one commit hash. main is the file .git/refs/heads/main, forty characters long. Creating a branch writes one such file, which is why it is instant however big the project is.
HEAD says where you are. Normally it holds the name of a branch, and when you commit, git creates the commit and moves that one branch to it. If HEAD holds a hash directly (a detached HEAD), commits still work but no branch follows them, so give them a name with git switch -c before you leave.
Merging brings another branch's work into yours. If your branch has no commits of its own since the other one split off, git just slides your pointer forward: a fast-forward. If both sides moved, git combines them from their merge base (the newest commit they share) and records a merge commit with two parents.
A conflict means both sides changed the same lines since the merge base. Git writes both versions into the file between <<<<<<<, ======= and >>>>>>>. You edit the file into what it should be, git add it and commit.
Rebase is the other way to combine work: it replays your commits one by one on top of a new base. The replayed commits are new objects with new hashes, and the history comes out as a straight line.
The rule that keeps rebase safe: only rebase commits that nobody else has built on. Merge is always safe because it only adds; rebase replaces.
Unit 03 · 3 lessons
Undoing things
Undo any mistake, and know which undo is safe to share
- 01Reset, restore, revert
Which of the three trees does each undo move?
- 02The reflog
Git remembers everywhere HEAD has been
- BOSSFind the commit that broke it
Binary search over history with git bisect
Read the unit guide
Almost nothing in git is ever really lost, because undoing is mostly moving pointers. The question is always which of the three places should change: the working directory, the index, or the branch.
For files, git restore file throws away edits in the working directory and git restore --staged file takes a file back out of the index. For commits, git reset moves the current branch to another commit: --soft moves only the branch, --mixed (the default) also resets the index, and --hard also overwrites your files.
Reset and git commit --amend rewrite history: the old commits are replaced by new ones. That is fine for commits nobody else has. For commits you have already shared, use git revert, which adds a new commit that does the opposite of an old one. History only grows, so nobody's work breaks.
The reflog is the safety net. Git records every position HEAD has had, so git reflog shows the commit you were on before a bad reset, rebase or branch deletion, and HEAD@{1} names where HEAD was one move ago. Entries last for weeks before garbage collection removes what nothing points at. The one thing it cannot return is work you never committed, so commit or git stash before anything destructive.
To find which commit broke something, git bisect runs a binary search over history: name a good commit and a bad one, test the commit git hands you, and repeat. A thousand commits take about ten tests, and git bisect run lets a script do them. git blame and git log -S answer the neighbouring questions: who last changed this line, and which commit added this text.
Unit 04 · 3 lessons
Working with others
Sync two repositories without losing anyone's work
- 01Remotes
origin, origin/main, fetch, pull and push
- 02Collaboration
Rejected pushes, pull, force and pull requests
- BOSSA clean history
Commit messages, tags, .gitignore, hooks and cherry-pick
Read the unit guide
Your repository and the one on the server are two separate databases. A remote is a nickname for the other one's address, and git clone names it origin. A clone has the whole history, so everything except syncing works offline.
origin/main is a remote-tracking branch: your copy's memory of where main was on origin the last time you talked to it. It only moves on fetch, pull and push. git fetch downloads new commits and moves origin/*; it never touches your branches or files. git pull is a fetch followed by a merge or a rebase of your branch. git push uploads your commits and asks origin to move its branch.
A push is accepted only if it fast-forwards the remote branch. If someone pushed first, yours is rejected: pull their commits in (git pull --rebase keeps the history straight), then push again. Forcing a push overwrites the remote branch. That is normal on a branch only you use after a rebase, with --force-with-lease, and never right on a shared branch.
A pull request is a conversation attached to a branch: a diff to review, checks that run on every push, and a button that merges it. Pushing more commits to the branch updates the request.
History is for readers. Make small commits whose first line says what changed, such as fix(auth): reject expired tokens, and use the body for why. Tags name releases and never move. .gitignore keeps build output and secrets from being added, but a secret that was ever pushed stays in old commits, so it must be revoked. Hooks are local scripts that can refuse a commit before it exists.
Learn Git Internals on your phone.
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.