Sub topic 1
Commits, branches, and HEAD
Prerequisites
This chapter assumes you can run git init, git add, git commit, git status, and git log in a terminal. You should also know that a branch is a named line of development. You do not need to know anything about git reset yet. This chapter builds the vocabulary that the later chapters use.
Why start here
git reset changes where a branch points. To predict what it does, you need a clear picture of what a commit is, what a branch is, and what HEAD is. Most reset mistakes come from a vague picture of these three things.
Commits form a chain
A commit is a snapshot of your tracked files, plus some metadata: the author, a message, a timestamp, and a pointer to its parent commit. A commit identifies itself by a hash, a 40-character ID computed from its contents. Git usually shows an abbreviated form, such as a1b2c3d.
Because each commit records its parent, commits form a chain. Commits are immutable. You cannot edit one in place. Commands that seem to "change" a commit actually create a new commit and leave the old one in the object database.
A branch is a movable pointer
A branch is a small file that holds the hash of one commit. It is not a copy of your files, and it does not hold a list of commits. The branch name points at the newest commit on that line of work, called the tip. Git finds the rest of the history by following parent pointers back from the tip.
When you make a new commit, Git does two things:
- It creates the new commit, with the current tip as its parent.
- It moves the current branch to point at the new commit.
Moving a branch pointer is cheap. This is the operation that git reset performs, and the chapters that follow build on it.
HEAD says where you are
HEAD is a special reference that names the commit you currently have checked out. Normally HEAD does not point at a commit directly. It points at a branch, and the branch points at a commit. Git calls this a symbolic reference.
You can see this with two commands.
git init demo && cd demo
echo one > file.txt
git add file.txt
git commit -m "first"
echo two >> file.txt
git commit -am "second"
echo three >> file.txt
git commit -am "third"
git log --oneline
cat .git/HEAD
git rev-parse HEAD maingit log --oneline lists three commits, newest first. The cat command prints ref: refs/heads/main (or master, depending on your Git configuration). git rev-parse HEAD main prints the same hash twice. This shows that HEAD resolves to the commit that the branch points at. If your default branch is not called main, replace the name in the last command.
When you commit, HEAD follows the branch, because it points at the branch and not at the commit.
Detached HEAD
If you check out a commit hash instead of a branch name, HEAD points directly at that commit. This is called a detached HEAD. New commits you make are not reachable from any branch, and Git may eventually discard them. Run git switch - to go back to your branch.
Relative names for commits
Git lets you name a commit relative to another one. This is how reset commands usually select a target.
| Name | Meaning |
|---|---|
| `HEAD` | The current commit |
| `HEAD~1` or `HEAD~` | The first parent of `HEAD`, one commit back |
| `HEAD~3` | Three commits back along first parents |
| `a1b2c3d` | The commit with that abbreviated hash |
| `main` | The commit the branch `main` points at |
The next chapters cover these names in more detail. For now, remember that HEAD~1 means "the commit before the current one."
Check your understanding
Exercise
You are on branch main with commits A, B, and C, where C is the newest. After you run git commit to create commit D, which things change: the main branch pointer, the HEAD file, or both?
Show answer
Only the main branch pointer changes. It moves from C to D. HEAD still contains a reference to main, so it follows the branch automatically without being rewritten.
Exercise
Why is a commit called immutable, and what does that imply about commands that appear to modify commits?
Show answer
A commit's hash is computed from its contents and its parent, so changing anything produces a different hash and therefore a different commit. Commands that appear to modify a commit create a new commit and move a branch pointer to it. The old commit still exists until Git garbage-collects it.