| name | Stacked Diffs Workflow |
| description | The agent implements stacked diffs (stacked PRs) for breaking large changes into reviewable chunks. Use when working on complex features, managing dependent changes, or optimizing code review flow. |
| category | methodology |
Stacked Diffs Workflow
Purpose
Stacked diffs (also known as stacked PRs or stacked branches) is a code review workflow pioneered at Meta (Facebook) that enables:
- Breaking large changes into small, reviewable chunks
- Parallel code review of dependent changes
- Faster iteration without waiting for reviews
- Cleaner git history with logical commits
- Reduced context switching for reviewers
Meta engineers use stacked diffs extensively with their internal tools, and this practice has spread to companies like Uber, Airbnb, and many others.
Features
| Feature | Description | Benefit |
|---|
| Dependent PRs | Chain of PRs building on each other | Logical separation |
| Parallel Review | Review all PRs simultaneously | Faster feedback |
| Easy Updates | Update any commit in the stack | Flexible iteration |
| Automatic Rebase | Tools handle rebase complexity | Less manual work |
| Merge Automation | Merge entire stacks efficiently | Cleaner history |
Stacked Diffs vs Traditional PRs
Traditional PR Workflow
Main ─────────────────────────────────────────────►
\
└── Feature Branch (1000 lines) ──────────►
│
└── Single large PR
- Hard to review
- Long review time
- All-or-nothing merge
Stacked Diffs Workflow
Main ────────────────────────────────────────────────►
\
└── PR 1: Add data model (100 lines)
\
└── PR 2: Add API endpoint (150 lines)
\
└── PR 3: Add frontend (200 lines)
\
└── PR 4: Add tests (150 lines)
Each PR:
- Small and focused
- Can be reviewed in parallel
- Merged independently (in order)
Tools
Graphite (Recommended)
npm install -g @withgraphite/graphite-cli
gt auth
gt branch create feature/add-data-model
git add . && gt commit -m "Add user data model"
gt branch create feature/add-api
git add . && gt commit -m "Add user API endpoints"
gt branch create feature/add-frontend
git add . && gt commit -m "Add user frontend components"
gt log
gt stack submit
gt stack restack
gt repo sync
ghstack (GitHub)
pip install ghstack
ghstack config set github.com
git checkout -b feature/user-system
git commit -m "Add user data model"
git commit -m "Add user API"
git commit -m "Add user frontend"
ghstack submit
git rebase -i HEAD~3
ghstack submit
ghstack land
git-branchless
cargo install git-branchless
git branchless init
git commit -m "Add data model"
git commit -m "Add API"
git commit -m "Add frontend"
git smartlog
git branchless submit
git branchless reword HEAD~2
git branchless submit
Sapling (Meta's Tool)
brew install sapling
sl clone https://github.com/org/repo
sl commit -m "Add data model"
sl commit -m "Add API"
sl commit -m "Add frontend"
sl smartlog
sl pr submit
sl goto <commit-hash>
sl amend
sl rebase -d <base>
Workflow Patterns
Creating a Stack
git checkout main && git pull
gt branch create feat/step-1-models
git add . && gt commit -m "feat: add user and post models"
gt branch create feat/step-2-api
git add . && gt commit -m "feat: add REST API for users"
gt branch create feat/step-3-frontend
git add . && gt commit -m "feat: add user management UI"
gt stack submit
Updating Middle Commits
gt branch checkout feat/step-2-api
git add . && git commit --amend
gt stack restack
gt stack submit
Handling Merge Conflicts
gt repo sync
gt stack restack --onto main
git add . && git rebase --continue
gt stack submit
Merging a Stack
gh pr merge 123 --squash
gt repo sync
gt stack restack
gh pr merge 124 --squash
gt repo sync
gt stack restack
gt stack merge
Best Practices
1. Keep PRs Small and Focused
## Good Stack Structure
PR 1: Database migrations and models
- Add User table migration
- Add Post table migration
- Add UserPost junction table
PR 2: Repository layer
- Add UserRepository with CRUD
- Add PostRepository with CRUD
PR 3: Service layer
- Add UserService with business logic
- Add PostService with business logic
PR 4: API controllers
- Add /users endpoints
- Add /posts endpoints
PR 5: Integration tests
- Add API integration tests
- Add E2E user flow tests
2. Write Clear Stack Descriptions
## PR Description Template for Stacks
### Stack Overview (in first PR)
This stack implements user authentication:
1. **PR 1** (this PR): Database schema for users
2. PR 2: Authentication service
3. PR 3: Login/Register endpoints
4. PR 4: JWT middleware
5. PR 5: Frontend auth components
### This PR
Adds the database schema and migrations for the user authentication system.
### Dependencies
- Base: `main`
- Next in stack: PR #124
### Testing
- [ ] Unit tests pass
- [ ] Migration tested locally
### Review Notes
Please review the schema design first. Later PRs depend on this structure.
3. Coordinate with Reviewers
const prNamingConvention = {
format: '[Stack: <stack-name>] <n>/<total>: <description>',
examples: [
'[Stack: user-auth] 1/5: Add user database schema',
'[Stack: user-auth] 2/5: Add authentication service',
'[Stack: user-auth] 3/5: Add login endpoints',
'[Stack: user-auth] 4/5: Add JWT middleware',
'[Stack: user-auth] 5/5: Add frontend components'
]
};
4. Handle Reviews Efficiently
gt branch checkout feat/affected-branch
git add . && gt commit --amend
gt stack submit
gt branch checkout feat/earliest-affected
git add . && gt commit --amend
gt stack restack
gt stack submit
Anti-Patterns
| Anti-Pattern | Problem | Solution |
|---|
| Huge PRs in stack | Still hard to review | Keep each PR < 400 lines |
| Unrelated changes | Confuses reviewers | Each PR should be cohesive |
| Too many PRs | Overhead exceeds benefit | 3-7 PRs is ideal |
| Unclear dependencies | Hard to merge correctly | Document stack structure |
| No tests per PR | Can't verify independently | Include relevant tests |
| Force pushing shared branches | Breaks collaborators | Use amend + restack |
Git Commands for Manual Stacking
When tools aren't available, you can manage stacks manually:
git checkout main
git checkout -b stack/feature-1
git checkout -b stack/feature-2
git checkout -b stack/feature-3
gh pr create --base main --head stack/feature-1
gh pr create --base stack/feature-1 --head stack/feature-2
gh pr create --base stack/feature-2 --head stack/feature-3
git checkout stack/feature-2
git rebase main
git push --force-with-lease
git checkout stack/feature-3
git rebase stack/feature-2
git push --force-with-lease
gh pr edit 124 --base main
Interactive Rebase for Stack Edits
git rebase -i main
git add . && git commit --amend
git rebase --continue
git push --force-with-lease origin stack/feature-1
git push --force-with-lease origin stack/feature-2
git push --force-with-lease origin stack/feature-3
IDE Integration
VS Code with Graphite
{
"graphite.enabled": true,
"graphite.showStackInStatusBar": true,
"git.branchPrefix": "stack/"
}
JetBrains IDEs
alias gs='gt stack'
alias gb='gt branch'
alias gc='gt commit'
Use Cases
1. Large Feature Development
## Stack: E-commerce Checkout Redesign
1. **PR 1**: Cart data model updates
- Add discount fields
- Add shipping options
2. **PR 2**: Cart service layer
- Discount calculation logic
- Shipping cost estimation
3. **PR 3**: Checkout API endpoints
- POST /checkout/initiate
- POST /checkout/complete
4. **PR 4**: Payment integration
- Stripe checkout session
- Webhook handlers
5. **PR 5**: Checkout UI components
- Cart summary
- Payment form
- Order confirmation
6. **PR 6**: E2E tests
- Full checkout flow tests
2. Refactoring Safely
## Stack: Extract Service from Monolith
1. **PR 1**: Add new service interface
- Define contracts
- Add feature flag
2. **PR 2**: Implement new service
- Core logic extraction
- Unit tests
3. **PR 3**: Add service client
- HTTP client implementation
- Circuit breaker
4. **PR 4**: Migrate callers (batch 1)
- Update user service
- Update order service
5. **PR 5**: Migrate callers (batch 2)
- Update remaining services
6. **PR 6**: Remove old code
- Delete deprecated methods
- Remove feature flag
3. Cross-Team Collaboration
gt branch create feature/base
git add . && gt commit -m "feat: add base infrastructure"
gt stack submit
gt branch checkout feature/base
gt branch create feature/extension
git add . && gt commit -m "feat: extend with new capability"
gt stack submit
gt repo sync
gt stack restack
Metrics and Benefits
Before Stacked Diffs
| Metric | Value |
|---|
| Average PR size | 1,200 lines |
| Review turnaround | 3-5 days |
| Merge conflicts | Frequent |
| Review quality | Lower |
After Stacked Diffs
| Metric | Value |
|---|
| Average PR size | 150-300 lines |
| Review turnaround | Same day |
| Merge conflicts | Rare |
| Review quality | Higher |
Related Skills
methodology/finishing-development-branch - Branch completion
methodology/requesting-code-review - Code review best practices
devops/github-actions - CI/CD for stacks
devops/feature-flags - Trunk-based development
Think Omega. Build Omega. Be Omega.