Stacked diffs
- Tier: Free, Premium, Ultimate
- Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated
- Status: Experiment
Use stacked diffs in the GitLab CLI to split a feature into small, dependent changes. Each stack contains diffs. Each diff is a commit on its own branch, built on the branch of the previous diff.
When you sync the stack, each diff becomes a merge request, and GitLab shows them as stacked merge requests. Reviewers can review and merge the earlier merge requests while you keep building on top of them.
The workflow for stacked diffs is:
- Build the stack locally: create a stack, then save each set of changes as a new diff, or amend an existing diff as you work.
- Sync the stack to GitLab: the GitLab CLI pushes your branches and creates or updates a merge request for each diff. GitLab chains the merge requests together as stacked merge requests. When you merge a merge request and delete its source branch, GitLab retargets the merge request above it.
The base command for this feature in the CLI is glab stack.
For all subcommands, see the glab stack commands.
This feature is an experiment. We’d love to hear your feedback in issue 7473.
Create a stack
Create a stack when you want to break a large feature into smaller, reviewable changes.
Prerequisites:
- You must have the GitLab CLI installed and authenticated.
To create a stack:
In your terminal, create a new stack and give it a name. For example:
glab stack create add-authenticationThe branch you have checked out becomes the base branch of the stack. If the base branch isn’t on GitLab yet, push it before you sync the stack.
Make your first set of changes in your editor.
Save your changes as the first diff:
glab stack save <file_name>Replace
<file_name>with the file to include in the diff. When prompted, enter a commit message that describes this change.Make your next set of changes and save them as a second diff:
glab stack save <file_name>Each time you run
glab stack save, you create a new diff and branch, and check out that branch. When prompted, enter a commit message that describes this change.When you’re ready to push your changes to GitLab and create merge requests, run:
glab stack sync
Your merge requests are available for review. You can continue creating more diffs in this stack, or switch to work on something else.
Add changes to a diff in a stack
To return to a specific diff in the stack to add more changes to it:
Optional. If the diff is in a different stack, switch to that stack:
glab stack switchDisplay a list of the diffs in the stack:
glab stack moveSelect the diff you want to edit and press Enter.
Make your changes.
Add the changes to the diff:
glab stack amend <file_name>When prompted, keep or update the description of the diff.
Push your changes:
glab stack sync
When you sync, the GitLab CLI:
- Rebases the diffs after the one you changed.
- Updates the merge requests for the changed diff and the diffs after it.
Choose between save and amend
Use the following commands for different purposes:
glab stack save: Creates a new diff (commit and branch). Use this when you’re adding a new logical change to your stack.glab stack amend: Adds your changes to the diff you have checked out. Use this when responding to review feedback or fixing an existing change.