How does git even work?

This is going to get a bit technical very quickly, and is most definitely targeted towards the computer science folk, but Git, at the data structure and algorithms level, uses a directed acyclic graph to store all of its data under the hood.

The magic of Git too, is that once you enter a directory and run git init, all it stores locally is a folder in that directory named .git, there is no Git 'server' or even a centralized location where it stores files (other than configuration, but that is optional)

To give an example, the very repository you are viewing as a website right now as seen from the command line:

More about said internals can be found here.