Sub topic 1
Commits, branches, and HEAD
git reset works by moving a pointer. Before you can predict what it does, you need a precise picture of what that pointer is and what it points at. This chapter builds that picture: what a commit contains, why a branch is only a label, and what HEAD is. The next chapter adds the index and the working directory.
Prerequisites
This chapter assumes you can already do the following:
- Run
git init,git add,git commit,git status, andgit login a terminal. - Create and switch branches with
git branchandgit switch(orgit checkout). - Read a short commit hash such as
a1b2c3dand know it identifies a commit.
You need Git 2.28 or later for the git init -b main option used below. On older versions, run git init and use whatever branch name it creates.
Commits are immutable snapshots
A commit is an object stored in Git's database. It records four things:
- A snapshot of the whole project, stored as a tree object. The snapshot is the complete state of every tracked file, not a diff.
- Zero or more parent commits. A normal commit has one parent, the first commit has none, and a merge commit has two or more.
- Author and committer details, with timestamps.
- The commit message.
Git computes the commit's name, its hash, from all of that content. Change any part, such as the message, the parent, or one byte in a file, and the hash changes. So you cannot edit a commit. Commands that appear to edit a commit, like git commit --amend and git rebase, create new commits and move a pointer to them.
Because each commit names its parent, commits form a chain. Git history is a directed graph in which every commit points back to the commit before it.
When git log shows a change, it is computing a diff by comparing a commit's snapshot with its parent's snapshot. Git stores the snapshots, and the diffs are derived from them.
Branches are movable labels
A branch is a name that points at one commit. In a normal repository, that is a small file under .git/refs/heads/ containing a commit hash. Nothing else is stored in it.
A branch does not contain commits. When people say "the commits on main", they mean every commit reachable by starting at the commit main points to and following parent links backward. The branch tip is the commit the branch name points to.
Making a commit does two things:
- Git creates a new commit whose parent is the current commit.
- Git moves the current branch label to the new commit.
Only step 2 changes a branch. The old commit is untouched. This distinction matters for reset, because git reset performs step 2 on its own, with no new commit created.
HEAD says where you are
HEAD is a special reference that marks your current position. It normally holds the name of a branch, not a commit hash. That makes it a symbolic reference: it points at a branch, and the branch points at a commit.
Two things follow:
- HEAD resolves to the commit at the tip of the checked-out branch. Git uses that commit as the parent for your next commit.
- When you commit, Git moves the branch that HEAD names. HEAD itself does not change.
In the diagram, arrows from commits point to parents. The branch labels are not part of any commit. They can move without changing a single commit.
See it in a real repository
Create a small repository with three commits. The later chapters reuse this setup.
git init -b main 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 --onelineThe log lists the three commits, newest first. Your hashes will differ from the ones shown here.
c3f1a9e third
7b2d4c0 second
e19a8f5 firstNow look at the raw pieces:
cat .git/HEAD
cat .git/refs/heads/main
git rev-parse HEAD main
git cat-file -p HEADTypical output:
ref: refs/heads/main
c3f1a9e5d0b7a46c2e8f91b3a7d4c60e5f2b8a11
c3f1a9e5d0b7a46c2e8f91b3a7d4c60e5f2b8a11
c3f1a9e5d0b7a46c2e8f91b3a7d4c60e5f2b8a11
tree 9d4e7c1a...
parent 7b2d4c0...
author ...
committer ...
thirdEach line shows one idea:
.git/HEADcontainsref: refs/heads/main. HEAD points to the branch, not to a commit..git/refs/heads/mainholds the full hash of the third commit.git rev-parseprints the same hash forHEADandmain, because HEAD resolves through the branch.git cat-file -p HEADshows the commit object: atree(the snapshot), aparent, the author data, and the message.
Reading .git files directly is good for learning. In day-to-day work, use git rev-parse and git symbolic-ref, because Git sometimes moves refs into a packed file (.git/packed-refs) and the loose file may not exist.
Creating a branch costs almost nothing
Create a branch at the current commit and look at the result:
git branch topic
git rev-parse main topicBoth names print the same hash. Git created one new ref file. No files were copied and no commits were made.
Now switch to topic and commit:
git switch topic
echo four >> file.txt
git commit -am "fourth"
git log --oneline --all --graph --decorateThe --decorate output shows HEAD -> topic on the new commit, with main still on third. The commit moved only the branch HEAD names. This is the model to hold on to: commits are permanent, labels move.
Detached HEAD
HEAD can also hold a commit hash directly. This is called a detached HEAD. It happens when you check out a commit, a tag, or a remote-tracking branch rather than a local branch:
git switch --detach HEAD~1
cat .git/HEAD
git branch --show-current.git/HEAD now contains a bare hash, and git branch --show-current prints nothing because no branch is checked out. Commits you make in this state have no branch label following them. If you switch away, nothing points at them. They stay in the repository for a while, and the reflog (covered in a later chapter) can still find them, but Git may eventually delete them as unreachable.
Return to your branch with git switch main.
Summary of the terms
| Term | What it is | Where it lives | When it changes |
|---|---|---|---|
| Commit | Immutable snapshot with parent links, author, and message | Object database in `.git/objects` | Never. New commits are created instead |
| Branch | Name pointing at one commit | `.git/refs/heads/<name>` | When you commit on it, or when you move it with reset |
| HEAD | Pointer to the current branch, or to a commit when detached | `.git/HEAD` | When you switch branches or check out a commit |
| Branch tip | The commit a branch currently points at | Value of the branch ref | Same as the branch |
Common errors in this model
- Treating a branch as a container. Deleting a branch deletes a label, not commits. The commits remain until they become unreachable and Git cleans them up. Merged work is safe because other labels reach it.
- Treating HEAD as a branch. HEAD usually names a branch, but it can name a commit. A command that "moves HEAD" while a branch is checked out will in practice move the branch HEAD points to. Reset relies on this behavior.
- Thinking a commit is a diff. A commit is a full snapshot. This is why Git can check out any commit quickly and why reset can restore the state of any commit.
- Expecting a commit to change when you amend it. The old commit is still there. Only the branch label has moved to a new commit.
- Committing in detached HEAD and then switching away. The commits are not on any branch. Create a branch (
git branch rescue) before you leave, or recover them from the reflog.
What this means for reset
Every idea in this chapter sets up one fact: a branch is just a label on a commit. git reset can pick up the current branch label and set it down on a different commit. The commits themselves stay where they are. Whether the files in your project change as well depends on the mode you choose, which the following chapters explain.
Exercise
In the demo repository, main points at commit C3 and topic was created at C3. You switch to topic, make one commit C4, and then run git switch main. Which commit does main point at, which commit does topic point at, and what does .git/HEAD contain?
Show answer
main still points at C3, because the commit on topic moved only the topic label. topic points at C4, whose parent is C3. .git/HEAD contains ref: refs/heads/main, because HEAD names the checked-out branch. You can confirm with git rev-parse main topic and cat .git/HEAD.
Exercise
You run git switch --detach HEAD~1, make a commit, and then run git switch main. Is the new commit lost immediately? What should you have done before switching away?
Show answer
It is not deleted immediately, but no branch or tag points at it, so it is unreachable and Git can remove it later. You can still find it with git reflog. Before switching away, run git branch rescue (or git switch -c rescue) to put a label on the commit so it stays reachable.