Git's DAG

June 03, 2019

Git is my personal favorite application of a directed acyclic graph. Git, while incredibly powerful, can be confusing because it is abstract. You do need, to a certain extent, understand its internals.

Objects are the unit of storage in Git. They are uniquely identified by the SHA-1 of its contents. They are immutable (i.e. they cannot be changed) [1].

Commit objects contain information about a revision, pointers to its parents, and additional metadata [1].

Gits Dag

Things to notice about the above example:

  • We have 2 branches, master and add-feature.
  • There are 6 commits.
  • Notice how a commit points at its parent. The initial commit, A, has no parent.

master, add-feature, and HEAD are examples of refs. A ref is a pointer to a commit (or to another ref). HEAD is a special ref, we’ll come back to this.

Note that when you are “checked out on branch add-feature”, what we really mean is “HEAD is pointing to the ref add-feature”.

Creating a Branch

git checkout -b add-feature

Branch Creation

This does the following: 1. Create a new ref, add-feature, pointing to the same commit that the currently checked out branch was pointing at. 2. Check out your new branch (point HEAD at the newly created ref).

Rebasing

git rebase master

Rebasing

The Rev List

git rev-list HEAD

Print the ancestry graph from the stated location (in reverse chronological order).

Rev List

I think I’ll leave it at that for now, I’m sure there will be more deep dives into Git in the future.

Sources

  1. Git Glossary