BONUS: Mastering Your GitLab Flow: The Power of Advanced Git Rebase
xanderekpl
Here is more advanced techniques for managing stacked branches that go beyond the manual rebase --onto dance.
The Next Generation of Rebase: git rebase --update-refs
This is arguably the most powerful, modern, and native Git solution for rebasing stacks. It was introduced in Git 2.38 (released in late 2022) and is designed specifically to solve the stacked branch problem without manual, repetitive rebase --onto commands.
Instead of rebasing one branch at a time, --update-refs allows you to move the entire stack in a single operation. Git understands the topological relationship between the branches and rebases them all, updating their relationships automatically.
Scenario: You have the same stack as before: main ← api-layer ← service-layer ← ui-component. main has new commits.
The Old Way (Recap):
git rebase main api-layergit rebase --onto api-layer <old-api-layer> service-layergit rebase --onto service-layer <old-service-layer> ui-component
The New Way with --update-refs:
You only need one command. From any branch in your stack (or even from main), you tell Git to rebase all branches that descend from your stack’s base (api-layer) onto the new main.
# Fetch the latest changes
git fetch
# From anywhere in your repo, execute this:
git rebase main api-layer --update-refs
What happens?
- Git identifies
api-layer,service-layer, andui-componentas a dependent stack. - It rebases
api-layeronto the latestmain. - It then automatically rebases
service-layeronto the new, rebasedapi-layer. - Finally, it rebases
ui-componentonto the new, rebasedservice-layer.
All intermediate refs are updated correctly. This single command replaces the entire manual sequence. It’s a game-changer for stacked diff workflows.