Use `git rebase` to replay your branch’s commits on top of another branch. A common use is updating a feature branch with the latest `main`.

```bash
git switch my-feature
git fetch origin
git rebase origin/main
```

Run this with a clean working tree. Git takes the commits from `my-feature` and reapplies them on top of `origin/main`, giving the replayed commits new IDs.

### If Git reports a conflict

Edit the conflicted files, then stage them and continue. Repeat if Git pauses on another commit.

```bash
git status
git add path/to/resolved-file
git rebase --continue
```

To cancel an in-progress rebase and return to where you started, run `git rebase --abort`.

### If you already pushed the branch

If it’s your own branch, update the remote with `git push --force-with-lease`. If others are working from it, coordinate before rebasing because their work may depend on the original commit IDs.

### To clean up your last few commits

Run `git rebase -i HEAD~3` to open the last three commits in your editor. Use `reword` to change a commit message or `squash` to combine a commit with the one above it.