Sub topic 1
Stacked Branches and `git rebase --update-refs`
Prerequisites
You should be comfortable with git add, git commit, git switch, git log, and single-branch rebases. You should know that a branch is a movable pointer to a commit and understand pull requests and remote branches. The examples require Git 2.38 or newer.
Branches are just pointers
A Git branch is a name that points to one commit. Creating a branch does not copy the repository. It creates another name for the current commit. New commits move the branch name forward.
For example, main and feature-1 can point to different commits:
A---B---C main
\
D---E feature-1A rebase creates new commits with new IDs. Git then moves the rebased branch pointer to those new commits.
What a stack of branches looks like
A stacked workflow splits a large change into smaller branches. Each branch starts from the previous branch:
git switch -c feature-1 main
# commit the first change
git switch -c feature-2
# commit the second change
git switch -c feature-3
# commit the third change
git log --oneline --decorate --graph main..feature-3The result is a linear stack:
main---A---B main
\
C feature-1
\
D feature-2
\
E feature-3Each branch can have its own pull request. Reviewers can examine smaller changes, while later branches depend on earlier ones.
The problem: rebasing the top leaves the rest behind
Suppose main receives a new commit. Rebasing only feature-3 changes its commits, but feature-1 and feature-2 still point to the old history. The branch names no longer describe one continuous stack.
A plain rebase updates the checked-out branch. It does not automatically move other local branch pointers.
Rebasing the whole stack with `--update-refs`
--update-refs moves eligible branch pointers that refer to commits being rewritten:
git fetch origin
git switch feature-3
git rebase --update-refs origin/mainGit rebases the commits and moves feature-1, feature-2, and feature-3 to their corresponding new commits.
Afterward, inspect the result:
git log --oneline --decorate --graph origin/main..feature-3Exercise
Why can rebasing only feature-3 leave feature-1 and feature-2 stale?
Show answer
A normal rebase moves only the branch being rebased. The other branch names still point to their old commits.