Git's DAG

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].

git 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

Terminal window
git checkout -b add-feature
git dag

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

Terminal window
git rebase master
rebasing

The Rev List

Terminal window
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

Wow! You read the whole thing. People who make it this far sometimes want to receive emails when I post something new.

I also have an RSS feed.