Sub topic 1
Refresher: commits, branches and HEAD are pointers
Prerequisites
This chapter assumes you can do the following:
- Make commits, create branches, and switch between them with
git switchorgit checkout. - Merge one branch into another.
- Run commands in a terminal and read their output.
You do not need to know anything about the reflog yet. This chapter only sets up the model that the rest of the tutorial relies on.
Why this model matters
Git rarely deletes commits when you "lose" work. A commit is usually still stored in the repository. What changes is that no branch points to it any more. To recover it, you need to know what points to what. That is the model this chapter covers.
Commits are snapshots linked by hashes
A commit is a stored snapshot of your project plus some metadata. The metadata includes the author, the message, and the hash of its parent commit. A hash is the long identifier Git computes from a commit's content, such as 9d4e7b8. Git usually shows only the first few characters.
Because each commit names its parent, commits form a chain. Commits never change once they are created. Operations that seem to "edit" history, such as git commit --amend or git rebase, create new commits and leave the old ones in place.
Branches are pointers
A branch is a small file that holds one commit hash. When you make a commit on a branch, Git creates the commit and then moves the branch pointer to it.
You can see this directly:
cat .git/refs/heads/main
git rev-parse mainBoth commands print the same hash. Some repositories pack their references into a single file, so the first command can fail even though the branch exists. git rev-parse works in every case.
HEAD points to where you are
HEAD is a reference to your current position. Normally it points to a branch name, and that branch points to a commit. This is called being attached to a branch.
cat .git/HEAD
# ref: refs/heads/mainIf you check out a commit hash directly, HEAD holds that hash instead of a branch name. This is a detached HEAD. Commits you make in this state are not on any branch, which later chapters cover in detail.
The diagram below shows the attached case.
What moves when you reset
A reset moves the current branch pointer to a different commit. Take a branch with commits A, B, C:
git reset --hard HEAD~2HEAD~2 means two commits before the current one. After this command, main points to A. Commits B and C still exist in the repository, but no branch points to them. Without a pointer, you cannot reach them by name.
The same idea explains other cases:
| Action | What happens to the pointers |
|---|---|
| `git commit` | The current branch moves forward to the new commit. |
| `git reset --hard <commit>` | The current branch moves to `<commit>`. Old commits stay stored. |
| `git branch -D feature` | The branch pointer is deleted. Its commits stay stored. |
| `git rebase main` | New copies of your commits are created, and the branch moves to the copies. |
| `git commit --amend` | A new commit replaces the old tip, and the branch moves to it. |
In each case the old commits are still in the repository, just without a branch pointing to them.
The missing piece
If you reset a branch and forget the old hash, you have no obvious way to find the old commit. git log only follows pointers from the current position, so it will not show commits that nothing points to.
Git solves this by keeping a local log of every place HEAD and each branch has pointed. That log is the reflog, and the next chapter introduces it.
Exercise
You run git branch -D feature on a branch with three unmerged commits. Are those commits deleted from the repository right away? Explain using pointers.
Show answer
No. git branch -D deletes only the branch pointer. The three commits remain stored, but nothing points to them, so git log cannot reach them. They can be recovered while they still exist, for example by creating a new branch at the last commit's hash.