Git internals, long

@tranquillity@mastodon.minionflo.net

Content warning: Git internals, long

So, first some terminology
The code files that a user edits are called the working tree
The characteristic of a git bare repo (if you heard of them) is that there's no working tree, just the .git directory
git add adds a file from the working tree into the index / staging area. The entire state of the index is stored in .git/index by default (though git does support using multiple indexes by setting GIT_INDEX_FILE)
The exact format (if that's interesting) is described here: git-scm.com/docs/index-format. An index is effectively equivalent to a tree object:
Git is primarily based on objects that link to each other. There's 4 types of objects: commit objects, tree objects, blob objects, and tag objects (for annotated tags, not that important for the core of things). These objects are indexed by the hash of their contents, and stored in .git/objects/ (there's a slight compression thingy on by default involved so that duplicate entries don't take up that much space, so eg. usually freshly cloned repos will have their objects packed, but a repo that was initialized locally will by default have the objects expanded on the filesystem)
blob objects are the simplest, those just contain zlib-compressed arbitrary data
tree objects gather together metadata about files or directories, and then link those to other objects. Files get linked to blob objects, directories to tree object. Each entry, besides the link to an object, gets a name and a mode (more or less mirrors perms and type of file and whatnot).
Lastly are commit objects, which contain a link to a tree object, one or more links to other commit objects, then an author and committer string (consisting of a name, an email, and a Unix timestamp + timezone). There's two, author roughly corresponds to who initially made the changes (so, who wrote a patch, or who pushed the changes to a repo after a git add + git commit + git push, etc. Basically the author of the tree object.), and comitter who committed the changes (so, basically the author of the commit object. This is modified by things like git rebase, which by default doesn't touch the author string), and lastly the commit message, which is just an arbitrary string, just with a few conventions (like eg. a key-value thingy that's parsed by various tools like Signed-off-by: somebody).
Then there's refs (in .git/refs/), which is basically named links to objects. They can either link to other refs, or directly to objects. There's also a few important refs in .git that are outside of .git/refs/, like .git/HEAD, that stores a ref to the current top commit object (aka where git usually starts walking the commit graph). Normally it is set to a ref to .git/refs/ (like refs/heads/master if I'm currently on the master branch), but in a "detached head" state it is set directly to a commit hash (happens when one checks out a commit or a tag with git checkout). This is what branches are - just refs.
Then we have remotes that control how git interacts with a remote server (for stuff like git pull / git push). First, there's a list of remotes in .git/config, then for each origin, the known branches (commit hashes) are stored in .git/refs/remotes/$ORIGIN_NAME/$BRANCH_NAME, + a .git/refs/remotes/$ORIGIN_NAME/HEAD file for the default branch (usually a ref to a branch on the origin, aka smth like refs/remotes/origin/master.
Also: each operation that's done using git commands gets saved to the reflog, to allow easily recovering from losing certain data (stored in .git/logs/)
Also: the process for creating a commit object consists of adding things to the index with git add, which slowly constructs a tree object (by first shoving the versions of the files into the object store as object files then recreating the dir structure out of tree objects linking to them) in .git/index, which then gets written into the object store with a git commit (or manually (aka with "plumbing", which are the more low-level git commands, as opposed to "porcelain" ones, which are the more high-level ones. Eg. git add vs git update-index) with just git write-tree. There's also git read-tree which takes a tree object hash and writes it into the index file, git mktree which creates a tree object out of a string in a similar format as git ls-tree).
Another thing are diffs; Those are generated by taking two tree objects and comparing them, then generating a .patch-like format describing how to turn one of them into the other. git diff can also compare between the trees referenced by commit objects, refs, and such.
There's also revs, which are basically a syntax for describing where to walk on the commit tree (described by gitrevisions(7)). This is important for eg. properly specifying what to include in patches generated by format-patch and such, cuz that can generate more than one .patch file. The plumbing commands for working with revs are git rev-parse, which basically normalize revs in the way controlled by its flags (eg. it will resolve references and such, can also be used for verifying if a rev string matches any commits filtered by something, etc), git rev-list that just lists the commits matched by the rev string, then git name-rev does basically the opposite of git rev-parse (turns commit hashes into revs matching some filter)
Then there's also things like git hash-object for serializing objects (blobs and commits - blob just takes the data, commit takes a string like the one printed by git cat-file -p on a commit object) from strings and such
git log walks the commit graph to try to order it into a list and then pretty-print the list, and also takes a rev string.