2 minutes
Git Animated 6
This is the sixth video in the Git Animated series. If you are interested in the other ones, check the links below:
Every previous video in this series stayed at the porcelain level: branches, merges, rebase, cherry-pick, all the commands you use day to day. This one goes one level deeper and shows what a commit actually is once you strip away the branch names and the pretty log graph.
Git stores everything as one of three tiny object types, each named by the SHA-1 hash of its own content: a blob (blue) is raw file content, nothing else, no filename attached. A tree (orange) is a directory listing, mapping filenames to blobs (or to other trees, for subdirectories). A commit (yellow) points at one tree, plus a message, an author, and, if it’s not the first commit, a parent pointing at the previous commit. A branch like main (green) is nothing more than a tiny file holding one commit’s hash, and HEAD (red) is usually just a pointer to a branch, not to a commit directly.
The video builds one file, hello.txt, all the way from a plain file on disk into a full commit: git hash-object -w turns its content into a blob, git add and git commit wrap that blob in a tree and a commit, and main/HEAD fade in pointing at the result. Then the file changes and gets committed again, and a second, entirely new blob/tree/commit chain appears, with the new commit’s parent pointing straight back at the first one. Nothing about the first commit is touched or rewritten, git only ever adds new objects; main and HEAD simply slide over to point at the newest one. That’s the mechanism every command in the earlier five videos, branching, merging, rebasing, cherry-picking, is built on top of.
A small legend in the corner keeps track of which color is which object type throughout.
For your convenience, here are the commands which appear in the video:
echo 'Hello, Git!' > hello.txt
git hash-object -w hello.txt
git add hello.txt
git commit -m 'Add hello.txt'
echo 'Hello, Git! v2' > hello.txt
git add hello.txt
git commit -m 'Update hello.txt'
Here is the video(there is no audio):
Feel free to drop me a comment or e-mail with your constructive criticism. It will be much appreciated!
Enjoy!