Apselin & Git
Git Git
Have you ever wondered exactly how Git keeps track of all those commits and branches behind the scenes? I keep trying to map out the commit graph in my head—maybe we could dissect it together and see if we can come up with a cleaner merge strategy.
Apselin Apselin
Yeah, Git is a puzzle of hashes and pointers. Each commit is just a blob that stores a snapshot, a tree of file objects, and pointers to its parents. Branches are just mutable pointers that move when you commit. When you merge, Git creates a new commit whose parents are the tips of the branches you’re merging, and the content is a union of their changes with conflict markers if they overlap. It’s basically a directed acyclic graph where the nodes are commits and the edges are parent links. If we map it out, we can see why fast‑forwards happen, why three‑way merges look the way they do, and maybe spot a simpler strategy for handling the usual merge headaches. Want to sketch a tiny example?
Git Git
Sure, let’s sketch a tiny one. Imagine you start with master at commit A. Then you branch to feature and make commits B and C. Meanwhile on master you make commit D. So we have: ``` A──D (master) \ B──C (feature) ``` Now merging feature into master would create a merge commit M with parents D and C. The graph becomes a diamond. We can walk through that and see exactly where conflicts could pop up. Want me to write that out?
Apselin Apselin
Sure thing, here’s the little map in plain text: ``` A──D (master) \ B──C (feature) ``` When you merge feature into master, Git will create a new merge commit M whose two parents are D and C. So the graph looks like a diamond: ``` A──D──M \ / B──C ``` Conflicts usually show up if D and C modified the same lines in the same files differently. If you walk through the three‑way merge, you’ll see where Git tries to combine the changes and where it can’t decide, prompting a conflict marker. Want me to break down that three‑way process step by step?
Git Git
Let’s walk through the three‑way merge step by step, then. We start with the two parent commits, D on master and C on feature, and the common ancestor A. Git takes the snapshot from A, then applies the changes from D relative to A, and then applies the changes from C relative to A. If the same line was edited in both D and C, Git can’t decide which edit is right, so it leaves conflict markers in the file. If the changes touch different lines, Git just combines them, creating a new commit M that points to both D and C. Once you see that flow, the conflict points become obvious, and you can decide whether to resolve by picking one side, merging manually, or letting Git handle it automatically. Want to dive into a specific file example?
Apselin Apselin
Yeah, that’s the core of it. Let’s say file `foo.txt` at A says “hello”, at D it changes to “hello world”, and at C it changes to “hi there”. Git pulls in “hello” from A, then applies the D change to get “hello world”, then tries the C change on that and sees it’s different, so it drops conflict markers: ``` <<<<<<< HEAD hello world ======= hi there >>>>>>> feature ``` You then pick which one you want or blend them. If the changes were on separate lines, Git would just stack them together with no conflict. So that’s the typical merge path. Anything you want to dig into?
Git Git
That’s a perfect illustration. If you’re curious, the next thing I’d like to dig into is how Git actually decides which side to keep when you choose a strategy like `ours` or `theirs`. It’s just a small function that tells the merge engine which version of the file to copy into the final tree. Or we could look at how the index is built during a merge, so you see the exact sequence of read‑write operations on the object database. Which one do you want to tackle first?
Apselin Apselin
I think the “ours” versus “theirs” angle is the easiest way to see what Git is actually doing under the hood, so let’s start there. The merge driver just looks at the two parent blobs and the base blob, and when you say “ours” it copies the whole blob from the current branch into the result, discarding the other side’s changes. “Theirs” does the opposite, pulling the blob from the other branch. It’s a simple copy step, but it gets you a good feel for how Git decides what ends up in the merge tree. Once we’re comfortable with that, we can walk through the index steps and see how Git stages the new tree before committing. Ready to dive into the code snippets?