Sub topic 1
How Git stores history: commits, HEAD, and branches
Prerequisites
This chapter assumes you can use git init or git clone, git add, git commit, git status, and git log in a terminal. You should know what a branch is and that a remote repository is a copy of the project stored elsewhere. Later chapters build on the model described here.
Commits are snapshots linked in a chain
A commit is a saved snapshot of your whole project at one moment. Each commit stores four things:
- The snapshot of all tracked files.
- The author, date, and message.
- A pointer to its parent commit, the one that came directly before it.
- A unique ID, a 40-character hash such as
a1b2c3d..., calculated from the content above.
Because the parent's ID is part of the content, changing any old commit would change its ID and the IDs of every commit after it. Git never edits a commit in place. When you "change" a commit, Git creates a new one. This fact explains most of the undo behavior in later chapters.
A merge commit is a commit with two parents, one for each line of history it joined.
Branches and HEAD are pointers
A branch is a small movable label that holds the ID of one commit. It is not a copy of the files. When you commit, Git creates the new commit, then moves the current branch label to it.
HEAD is a pointer that marks where you are now. Normally HEAD points to a branch name, and the branch points to a commit. The commit that HEAD reaches is the one your next commit will use as its parent.
Arrows point from a commit to its parent, so the chain reads backward in time.
Seeing the pointers
You can view this structure with git log:
git log --oneline --decorate -4Typical output looks like this:
c3f9e21 (HEAD -> main, origin/main) Add login form
8d41b7a Fix typo in README
5e0a9c4 Add user model
1b7d2f0 Initial commitHEAD -> main shows that HEAD points at the branch main. The name origin/main is a remote-tracking branch, a label that records where main was on the remote the last time you fetched.
You can also refer to commits relative to HEAD:
| Name | Meaning |
|---|---|
| `HEAD` | The current commit |
| `HEAD~1` or `HEAD~` | The parent of the current commit |
| `HEAD~2` | The grandparent, two commits back |
| `HEAD@{1}` | Where HEAD pointed one move ago (from the reflog) |
The ~ forms follow the chain of parents. The @{} form follows your own movement history, which a later chapter covers.
Moving the pointers
Most undo commands do one of two things. They either move a branch label to a different commit, or they add a new commit. Keeping this in mind makes the commands easier to predict.
You can try it safely on a throwaway repository:
git init pointer-demo
cd pointer-demo
git commit --allow-empty -m "first"
git commit --allow-empty -m "second"
git log --oneline
git branch backup
git log --oneline --decorateThe git branch backup command creates a second label on the same commit. Both main and backup now name the commit "second". No files were copied, which shows how cheap branches are. A label like backup is also a simple safety net before a risky command.
Detached HEAD
If you check out a commit ID directly, such as git checkout 5e0a9c4, HEAD points at that commit and not at a branch. This is called a detached HEAD. New commits made in this state have no branch label, so they are easy to lose when you switch away. Create a branch with git switch -c new-name to keep them.
Exercise
In a repository, git log --oneline --decorate shows 9f2a1cc (HEAD -> main) Add search. You run git commit -m "Add filters". Which pointers change, and does the commit 9f2a1cc change?
Show answer
A new commit is created whose parent is 9f2a1cc. The branch main moves to the new commit, and HEAD still points to main, so it now reaches the new commit too. Commit 9f2a1cc does not change, because commits are never edited in place.
Exercise
What is the difference between HEAD~2 and HEAD@{2}?
Show answer
HEAD~2 follows parent links and names the grandparent of the current commit. HEAD@{2} uses the reflog and names where HEAD pointed two moves ago, which may be unrelated to the commit's parents.