Sub topic 5
Reading update-ref lines in the todo list
This chapter assumes you have used git rebase -i on a single branch and know that --update-refs moves every branch in a stack. Here you will see what the option writes into the todo list, and how editing that list changes the result.
Where the lines come from
Start the rebase with the interactive flag and the update option:
git switch feature-3
git rebase -i --update-refs origin/mainGit opens your editor with the commits it will replay. For each commit that another local branch points to, Git inserts an update-ref line right after it. With the three-branch stack from earlier chapters, the todo list looks like this:
pick 1a2b3c4 Add parser skeleton
update-ref refs/heads/feature-1
pick 5d6e7f8 Add parser tests
pick 9a8b7c6 Wire parser into CLI
update-ref refs/heads/feature-2
pick 0f1e2d3 Document CLI flagRead it from top to bottom, the way Git runs it:
- Git replays
Add parser skeleton. This is the tip offeature-1, so the next line names that branch. - Git replays the two middle commits. The second one is the tip of
feature-2, sofeature-2is named next. - Git replays the last commit.
feature-3has noupdate-refline. It is the branch you checked out, and the rebase moves it anyway when it finishes.
What the command does
An update-ref line is not a commit. It tells Git to remember the commit it has just produced as the new position for that branch. When the rebase finishes, Git moves each named branch to the commit it recorded. It then prints which refs it updated. If the rebase stops on a conflict, the branches have not moved yet. Aborting with git rebase --abort therefore leaves them where they were.
Editing the lines
The todo list is plain text, so you can change what the rebase does to each branch.
- Delete a line to leave that branch alone. It keeps pointing at its old commit, which is rarely what you want in a stack.
- Move a line to a different spot in the list. The branch ends up at whichever commit comes just before the line.
- Reorder commits with their
picklines as usual. Keep eachupdate-refline next to the commit it should follow, or the branch will end up in the wrong place. - Change a command, for example
picktosquashorreword. Theupdate-refline still works, because Git records whatever commit exists at that point.
A branch that is checked out in another worktree gets no update-ref line. Git will not move it. The next chapters return to that limit.
Checking the result
After the rebase, confirm that each branch moved:
git log --oneline --decorate --graph origin/main..feature-3Every branch name should sit on a commit in the new history. If one is missing or still sits on an old commit, its update-ref line was probably deleted or moved.
Exercise
You run git rebase -i --update-refs origin/main on feature-3, and the todo list contains an update-ref refs/heads/feature-1 line. You delete that line and finish the rebase. Where does feature-1 point afterwards, and why is that a problem for the stack?
Show answer
feature-1 still points at its old commit from before the rebase. Git only moves branches that have an update-ref line. feature-2 and feature-3 now sit on rewritten commits while feature-1 sits on the old base, so the stack is no longer a linear chain.