Use when working with stacked diffs (branch B based on branch A, which is based on main).
Use this skill when working with stacked diffs (Branch B based on Branch A, which is based on main).
This skill covers two scenarios:
Use when Branch A (the base) has new commits and Branch B needs to be updated to include them.
# You should be on Branch B
git branch --show-current
# Fetch latest
git fetch origin
# See Branch A's recent commits
git log --oneline origin/branch-a -10
Find where Branch B originally diverged from Branch A:
# This shows the commit where Branch B was created from Branch A
git merge-base HEAD origin/branch-a
The --onto flag lets you transplant Branch B's unique commits onto the updated Branch A:
# Syntax: git rebase --onto <new-base> <old-base> <branch>
git rebase --onto origin/branch-a $(git merge-base HEAD origin/branch-a) HEAD
Or if you know the old base commit:
git rebase --onto origin/branch-a <old-base-commit> HEAD
If Branch A's changes conflict with Branch B's changes:
git add <files>git rebase --continuegit push --force-with-lease origin branch-b
# On branch-b, which was based on branch-a at commit abc123
# branch-a now has new commits
$ git fetch origin
$ git rebase --onto origin/branch-a abc123 HEAD
Successfully rebased and updated refs/heads/branch-b.
$ git push --force-with-lease origin branch-b
If Branch B hasn't diverged much and you're okay with a linear history:
git rebase origin/branch-a
This works well when Branch A only added commits (no force-pushes or rebases).
Use when Branch A has been merged to main, and Branch B needs to be rebased onto main (removing the now-redundant Branch A commits).
git log shows commits from Branch A in Branch B's history# Check current branch status
git status
# See how the branch relates to main
git log --oneline --graph HEAD~20..HEAD
# Check if the PR is in a conflicting state
gh pr view --json mergeable,mergeStateStatus
If you see "mergeStateStatus": "DIRTY" or "mergeable": "CONFLICTING", proceed.
git fetch origin main
# Start rebase onto main
git rebase origin/main
When git tries to apply commits that are already in main (via the merged base branch), you'll see conflicts. These commits should be skipped, not resolved.
Signs a commit should be skipped:
For each conflicting commit that was already merged:
git rebase --skip
Git will automatically drop some commits with messages like:
dropping abc123 Some commit message -- patch contents already upstream
This is expected and correct.
If you encounter a conflict in code that is genuinely new to this branch:
git add <files>git rebase --continuegit push --force-with-lease origin <branch-name>
gh pr view --json mergeable,mergeStateStatus
gh pr checks
The PR should now show:
"mergeable": "MERGEABLE""mergeStateStatus": "BLOCKED" (waiting for CI) or "CLEAN" (ready to merge)$ git rebase origin/main
Rebasing (1/15)
CONFLICT (content): Merge conflict in src/feature.py