Sub topic 1
How commits and branch history fit together
Prerequisites
This chapter assumes you can use git add, git commit, git branch, git log and git status. You should be comfortable on the command line and know what a remote branch is. Nothing here changes your repository. It builds the mental model that the later chapters on git rebase -i rely on.
Commits are snapshots linked by parents
A commit is a record that stores three things: a snapshot of your project files, a message with author details, and a pointer to its parent commit. The parent is the commit that came directly before it. Git identifies each commit by a hash, a long hexadecimal ID computed from the commit's contents. The short form, such as 3f2a1b9, is usually enough to refer to one.
Because the parent's hash is part of the data that produces the child's hash, commits form a chain. Change any commit and its hash changes. Every commit after it must then change too, because each one names its parent by hash.
The arrows show the order in which commits were made. In Git's storage, each arrow is really the child pointing back at its parent.
Branches are movable labels
A branch is a name that points to one commit, called its tip. The history of a branch is the tip plus every ancestor you reach by following parent pointers. A branch does not contain commits. It names one, and the chain behind that commit defines the history.
When you make a new commit, Git creates the commit with the current tip as its parent. Then it moves the branch label forward to the new commit. HEAD is a special pointer that records which branch you have checked out, so Git knows which label to move.
You can see this with git log:
git log --oneline --graph -n 5The newest commit appears first. Each line below it is its parent, then its grandparent, and so on.
Why this matters for rebase
Interactive rebase works on a range of commits: those on your branch that are not yet on the base you choose. The range is defined by the chain of parents. Later chapters show how to pick the base. For now, the point is that a branch's history is only a chain, and the chain can be rebuilt.
When rebase rewrites a commit, it cannot edit the original. The hash would no longer match the content. Instead it creates a new commit with the changed content, then new copies of every commit after it, and finally moves the branch label to the new tip. The old commits still exist in the repository for a while. They are just no longer reachable from the branch name.
Reading history before you rewrite it
Before you rewrite anything, check what is on your branch and what is already shared. These commands show both:
git status
git log --oneline origin/main..HEAD
git log --oneline HEAD..origin/mainThe A..B form lists commits reachable from B but not from A. The first git log command shows your local commits that origin/main does not have. The second shows commits on origin/main that you do not have yet. Commits that exist only on your machine are safe to rewrite. Commits that others have already fetched are not, because rewriting them gives you different hashes from the copies they hold.
Common mistakes
- Treating a commit as editable. Any change produces a new commit with a new hash.
- Assuming a branch holds its commits. It only points at the tip.
- Forgetting that every descendant of a changed commit changes as well.
- Rewriting commits that teammates have already pulled.
Exercise
A branch feature has commits A, B, C, D in that order, with D as the tip. You rewrite commit B in some way. Which commits get new hashes, and why?
Show answer
B, C and D get new hashes. B changes because its content changed. C names B as its parent, so C's hash input changes, and the same applies to D. Commit A stays the same because it comes before B and does not depend on it.
Exercise
What does git log --oneline origin/main..HEAD show, and why is it useful before rewriting history?
Show answer
It lists the commits reachable from HEAD that are not reachable from origin/main. These are the commits you have made locally and not yet shared, so they are the ones you can usually rewrite safely.