import DefinitionCard from "@site/src/components/DefinitionCard";

# Git Rebase Onto

_Replay a chosen range of commits on a new base, not every commit since the branches split._

A plain `git rebase main` replays every commit on your branch that `main` does not have. `git rebase --onto` lets you say where that range starts. You use it when the branch carries commits you no longer want, such as a parent branch that was squash merged.

## Introduction

[Rebase](./merge-vs-rebase.mdx) takes a list of commits and recreates them on top of another commit. Without `--onto`, Git builds that list from one argument and uses the same argument as the new base. That works while the base branch still contains your branch's history. It fails when the base was rewritten, because Git can no longer tell which commits belong to you.

`--onto` separates the two jobs. One argument says where the commits go. Another says which commits to leave behind.

## Understanding the Concept

The full form takes three arguments:

```bash
git rebase --onto   
```

Git first checks out ``. It then collects the commits in `..`, resets the branch to ``, and applies those commits one at a time. Leave out `` and Git uses the current branch.

| Command | Commits replayed | Placed on |
| --- | --- | --- |
| `git rebase main` | `main..HEAD` | `main` |
| `git rebase --onto main part-1` | `part-1..HEAD` | `main` |
| `git rebase --onto main part-1 part-2` | `part-1..part-2` | `main`, after checking out `part-2` |
| `git rebase --onto HEAD~2 HEAD~1` | `HEAD~1..HEAD` | `HEAD~2`, which drops the commit at `HEAD~1` |

The [Git documentation](https://git-scm.com/docs/git-rebase) states that `` "May be any valid commit, and not just an existing branch name." The same holds for ``. When the old parent branch is gone, pass its last commit hash from the [reflog](./git-reflog.mdx) instead.

## Applying It in Practice

**Moving a branch off a squash-merged base.** `part-2` was built on `part-1`. `part-1` then landed on `main` as one squashed commit. The examples below ran on Git 2.43.0 in a scratch repository:

```text
$ git log --oneline --graph --all
* 273b8f8 Add parser (#1)
| * bbbd1d8 Document CLI flag
| * cf39507 Add CLI flag
| * 68ab8cb Fix parser review comments
| * b09391d Add parser
|/
* 94784c9 Initial commit
```

`main` has the squashed commit `273b8f8`, but not `b09391d` or `68ab8cb`. A plain rebase therefore tries to replay all four commits and stops on the first one:

```text
$ git rebase main
Auto-merging a.txt
CONFLICT (add/add): Merge conflict in a.txt
error: could not apply b09391d... Add parser
```

Name `part-1` as the upstream so its commits stay behind:

```text
$ git rebase --onto main part-1 part-2
Successfully rebased and updated refs/heads/part-2.
$ git log --oneline --graph main part-2
* 8a2ca3a Document CLI flag
* 334a206 Add CLI flag
* 273b8f8 Add parser (#1)
* 94784c9 Initial commit
```

Only the two `part-2` commits moved. [Rebase a Stacked Branch After a Squash Merge](/learn/answers/rebasing-and-stacks/rebase-stack-after-squash-merge) is the short fix for this case.

**Moving a whole stack.** With three branches stacked as `part-1`, `part-2`, `part-3`, a plain `--onto` from the top moves only `part-3`. `part-2` keeps pointing at the old commits. Add `--update-refs` and Git moves every local branch inside the rebased range:

```text
$ git switch part-3
$ git rebase --onto main part-1 --update-refs
Successfully rebased and updated refs/heads/part-3.
Updated the following refs with --update-refs:
	refs/heads/part-2
```

Push each moved branch with [`--force-with-lease`](./force-with-lease.mdx). [Rebase a Whole Stack With --update-refs](/learn/answers/rebasing-and-stacks/git-rebase-update-refs) covers making that option the default.

**Dropping commits.** `git rebase --onto HEAD~2 HEAD~1` replays only the top commit onto `HEAD~2`, which removes the commit in between:

```text
$ git log --oneline -3
5a893da Real fix
7901c07 Debug logging
2c6584e Add parser (#1)
$ git rebase --onto HEAD~2 HEAD~1
Successfully rebased and updated refs/heads/main.
$ git log --oneline -2
8bae080 Real fix
2c6584e Add parser (#1)
```

## Engineering Considerations

Pick the upstream with care. It must be the last commit you want to leave out. Pass the wrong one and Git either replays commits that already landed or silently drops some of your own. Run `git log --oneline ..` first. That command lists exactly the commits the rebase will replay.

Rebase rewrites commit IDs. Every commit after the new base gets a new hash, as `cf39507` became `334a206` above. Anyone who based work on the old commits has to rebase too, so restrict rewrites to branches you own or a [stack of pull requests](./stacked-prs.mdx) you restack together.

`--update-refs` skips any branch checked out in another worktree. The Git documentation says so directly. Agents working in separate [Git worktrees](./git-worktrees.mdx) keep their branches on the old commits until each one rebases on its own.

## Scaling and Operations

A stack of five branches means four `--onto` decisions after each squash merge, and each needs the right upstream. Teams that run several agents at once hit this daily. The usual answer is a tool that records each branch's parent, so the upstream is never guessed.

Treq records that parent for you. A [workspace](/docs/concepts/workspaces) can target another workspace's branch. When the pull request below merges, the workspace above moves onto the merged workspace's own target, with only its own commits. That is the same result as `git rebase --onto`, without picking the upstream by hand.

## Next Steps

- [Merge vs Rebase](./merge-vs-rebase.mdx): when to replay commits and when to join histories
- [What are Stacked PRs?](./stacked-prs.mdx): build and restack dependent branches
- [Cherry-pick vs Rebase](./cherry-pick-vs-rebase.mdx): copy single commits instead of moving a branch
- [Auto-rebase AI Branches](/learn/how-to/auto-rebase-ai-branches): keep agent branches current as their base moves
